Just My Life & My Work

[Objective C] NSNumber

來練習數值物件NSNumber吧!

  • 基本資料型態必須先轉為 NSNumber 類別,才能存為 Foundation framework 的數值物件。如 int 轉為 NSInteger。
  • 常用資料型態以及其方法:
  • 資料型態 初始方法 讀取方法 自動配置
    int initWithInt intValue numberWithInt
    float initWithFloat floatValue numberWithFloat
    double initWithDouble doubleValue numberWithDouble
    char initWithChar charValue numberWithChar
    BOOL initWithBool boolValue numberWithBool
  • -(BOOL) isEqualToNumber: (NSNumber *) number2
    若前者的數值等於後者,則回傳 YES,否則回傳 NO
  • -(NSComparisonResult) compare: (NSNumber *) number2
    前者和後者比較後會回傳 NSComparisonResult 型態的值,若前者小於後者則回傳 NSOrderedAscending,等於則回傳 NSOrderedSame,若大於則回傳 NSOrderedDescending
  • NSComparisonResult 的列舉型態:
  • 回傳值 列舉的代表數值
    NSOrderedAscending -1
    NSOrderedSame 0
    NSOrderedDescending 1

範例程式:

//
//  main.m
//  HappyMan's Program - NSNumber
//  Xcode 4.5
//  Created by ShengWen on 12/10/10.
//  Copyright (c) 2012年 ShengWen. All rights reserved.
//  https://cg2010studio.wordpress.com/

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {

    NSNumber *intNumber = [[NSNumber alloc] initWithInt:100];
    NSNumber *floatNumber = [[NSNumber alloc] initWithFloat:99.9];
    NSNumber *doubleNumber = [[NSNumber alloc] initWithDouble:100.0];
    NSNumber *charNumber = [[NSNumber alloc] initWithChar:'T'];
    NSNumber *boolNumber = [[NSNumber alloc] initWithBool:TRUE];

    // 列印各個數值
    NSLog(@"intNumber = %i", [intNumber intValue]);
    NSLog(@"floatNumber = %g", [floatNumber floatValue]);
    NSLog(@"doubleNumber = %g", [doubleNumber doubleValue]);
    NSLog(@"charNumber = %c", [charNumber charValue]);
    if ([boolNumber boolValue]) {
        NSLog(@"boolNumber is TRUE");
    }else {
        NSLog(@"boolNumber is FALSE");
    }

    // 自動配置與初始化
    NSNumber* intNumber2 = [NSNumber numberWithInt:INT_MAX];
    NSNumber* floatNumber2 = [NSNumber numberWithFloat:FLT_MIN];
    NSNumber* doubleNumber2 = [NSNumber numberWithDouble:DBL_MAX];
    NSLog(@"intNumber2 = %i",[intNumber2 intValue]);
    NSLog(@"floatNumber2 = %g",[floatNumber2 floatValue]);
    NSLog(@"doubleNumber2 = %g",[doubleNumber2 doubleValue]);

    // isEqualToNumber 函式
    if ([intNumber isEqualToNumber: doubleNumber] == YES) {
        NSLog(@"intNumber 和 doubleNumber 相等");
    }else {
        NSLog(@"intNumber 和 doubleNumber 不相等");
    }

    // compare 函式
    NSComparisonResult result = [intNumber compare:doubleNumber];
    if (result == NSOrderedAscending) {
        NSLog(@"intNumber 和 doubleNumber 為遞增");
    }else if (result == NSOrderedDescending) {
        NSLog(@"intNumber 和 doubleNumber 為遞減");
    }else {
        NSLog(@"intNumber 和 doubleNumber 為相同順序");
    }

    return 0;
}

執行結果:

2012-10-10 12:38:08.473 HappyMan’s First Program[92405:403] intNumber = 100

2012-10-10 12:38:08.475 HappyMan’s First Program[92405:403] floatNumber = 99.9

2012-10-10 12:38:08.476 HappyMan’s First Program[92405:403] doubleNumber = 100

2012-10-10 12:38:08.477 HappyMan’s First Program[92405:403] charNumber = T

2012-10-10 12:38:08.477 HappyMan’s First Program[92405:403] boolNumber is TRUE

2012-10-10 12:38:08.478 HappyMan’s First Program[92405:403] intNumber2 = 2147483647

2012-10-10 12:38:08.479 HappyMan’s First Program[92405:403] floatNumber2 = 1.17549e-38

2012-10-10 12:38:08.480 HappyMan’s First Program[92405:403] doubleNumber2 = 1.79769e+308

2012-10-10 12:38:08.484 HappyMan’s First Program[92405:403] intNumber doubleNumber 相等

2012-10-10 12:38:08.485 HappyMan’s First Program[92405:403] intNumber doubleNumber 為相同順序

參考:Objective-C 數值物件NSNumber Class Reference

隨意留個言吧:)~

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com 標誌

您的留言將使用 WordPress.com 帳號。 登出 /  變更 )

Google photo

您的留言將使用 Google 帳號。 登出 /  變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 /  變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 /  變更 )

連結到 %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

標籤雲

%d 位部落客按了讚: