NSNumber(封裝基本數據類型)
//
// main.m
// OC05-task-04
//
// Created by Xin the Great on 15-1-26.
// Copyright (c) 2015年 Xin the Great . All rights reserved.
//
#import
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
//////////////////////NSNumber//////////////////////
//封裝基本數據類型
int intValue = 100;
float floatValue = 3.14;
BOOL boolValue = YES;
NSNumber *intNumber = [[NSNumber alloc] initWithInt:intValue];
NSLog(@"intNumber is %@", intNumber);
NSNumber *floatNumber = [NSNumber numberWithFloat:floatValue];
NSLog(@"floatNumber is %@", floatNumber);
NSNumber *boolNumber = [NSNumber numberWithBool:boolValue];
NSLog(@"boolNumber is %@", boolNumber);
//包裝之後就可以放到容器中
NSArray *arr = @[intNumber, floatNumber, boolNumber];
NSLog(@"arr is %@", arr);
//還原成基本數據類型
float value = [floatNumber floatValue];
int value2 = [intNumber intValue];
NSLog(@"value is %.2f",value);
NSLog(@"value2 is %d",value2);
//快速創建的方法
NSNumber *intNum = @80;//----> NSNumber *intNumber = [[NSNumber alloc] initWithInt:80];
NSNumber *floatNum = @3.14;// ---->[NSNumber numberWithFloat:3.14];
NSNumber *boolNum = @YES;// ---> [NSNumber numberWithBool:YES];
NSNumber *charNum = @'a'; // ---> [NSNumber numberWithChar:'a'];
//運算
NSNumber *number = @(1 + 2);
//直接在數組中寫
NSArray *arr1 = @[@"string", @12, intNum, floatNum, boolNum, charNum, number];
NSLog(@"arr1 is %@", arr1);
}
return 0;
}