OC -- NSNumber

2016-01-23  本文已影响0人  J_coco

基本数据类型:
int long NSInteger
float double
BOOl
由于不是对象类型,所以OC中的数组和字典都不能保存

NSNumber -> 将任意基本数据类型封装成对象类型

//1.创建
  //基本数据类型 ===>  对象

//空
NSNumber *number = [[NSNumber alloc]init];

//int
NSNumber *int_ =  [[NSNumber alloc]initWithInt:10];

//float
NSNumber *float_ = [[NSNumber alloc]initWithFloat:3.14];

//double
NSNumber *double_ = [[NSNumber alloc]initWithDouble:3.14];

//BOOL
NSNumber *bool_ [[NSNumber alloc]initWithBool:YES];

//char
NSNumber *char_ = [[NSNumber alloc]initWithChar:'C'];

NSLog(@"%@",int_);


//2.快速创建:@...
NSNumber *_int = @100;
NSNumber *_float = @3.14;
NSNumber *_bool = @YES;
NSNumber *_char = @'c';
NSNumber *_double = @(3.14 * 6 + 2.5);


//3.NSNuber可以放入容器中
NSArray *array = @[_int,_float,_bool,_char];
NSDictionary *dic = @{@"key1":_int,@"key2":_double};


//4.NSNumber ==>基本数据类型
int a = [_int intValue];
float b = [_float floatValue];
char c = [_char charValue];
double = [_double doubleValue]
上一篇下一篇

猜你喜欢

热点阅读