Foundation - NSNumber

NSNumber *num = [NSNumber numberWithInt:10];

NSDictionary *dict = @{
  @"name" : @"jack",
  @"age" : num
};

NSNumber *num2 = dict[@"age"];

int a = [num2 intValue];

NSLog(@"%d" , a); //10

// @20 将 20包装成一个NSNumber对像

NSArray *array = @[
  @{@"name" : @"jack", @"age" : @20},
  @{@"name" : @"rose", @"age" : @25},
  @{@"name" : @"jim", @"age" : @27}
];

// 将各种基本数据类型包装成NSNumber对象
@10.5;
@YES;
@'A'; // NSNumber对象
@"A"; // NSString对象

// 将age变量包装成NSNumber对象
int age = 100;
@(age);
//[NSNumber numberWithInt:age];

NSNumber *n = [NSNumber numberWithDouble:10.5];

int d = [n doubleValue];

int a = 20;

// @"20"
NSString *str = [NSString stringWithFormat:@"%d", a];
NSLog(@"%d", [str intValue]);

 

 

posted @ 2013-09-19 13:40  tzktzk1  阅读(140)  评论(0编辑  收藏  举报