现在的位置: 首页 > 综合 > 正文

NSNumber、NSValue和NSNull

2014年02月11日 ⁄ 综合 ⁄ 共 2796字 ⁄ 字号 评论关闭

NSNumber、NSValue和NSNull

NSArray和NSDictionary是不支持存储基本数据类型的,所以Cocoa提供了NSNumber类用来包装基本的数据类型,如:int、char、BOOL、float..等各种有符号和无符号的基本数据类型

NSNumber

1、

+ (NSNumber *)numberWithChar:(char)value;

+ (NSNumber *)numberWithInt:(int)value;

+ (NSNumber *)numberWithFloat:(float)value;

+ (NSNumber *)numberWithBOOL:(BOOL)value;

...

...

创建NSNumber对象

NSNumber *number;

number = [NSNumber numberWithInt:123];

 

 

2、

- (char)charValue;

- (int)intValue;

- (float)floatValue;

- (BOOL)boolValue;

...

...

提取数值

[number intValue];

 

创建了NSNumber之后,就可以把它存储到NSArray或者NSDictionary中了:

NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];

[array addObject:number];//存储到数组中

可以用快速枚举遍历数组元素

for(NSNumber *num in array)

{

  NSLog(@"%c", [num intValue]);

}

 

NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:10];

[dic setObject:number forKey:@"1"];//存储到字典中

可以用快速枚举遍历字典元素

for(id mdic in dic)

{

  NSLog(@"%c", [[dic objectForKey:mdic] intValue]);

}

通常,将一个基本类型的数据包装成对象叫做装箱(boxing),从对象中取出基本数据类型叫做取消装箱(unboxing)

 

NSValue

实际上NSMunber是NSValue的子类,NSValue可以包装任意一个对象,可以用NSValue将struct存到NSArray和NSDictionary中。

 

1、+ (NSValue *)valueWithBytes:(const void *)value
objCType:(const char *)type;

创建一个NSValue

value:对象地址

objCType:通常是一个用来描述对象类型和大小的字符串,@encode可以接受一个数据类型的名称自动生成一个合适的描述字符串

 

2、- (void)getValue:(void *)value;

提取数值

 

3、Cocoa提供了常用struct数据类型转换成NSValue的便捷方法:

+ (NSValue *)valueWithPoint:(NSPoint)point;

+ (NSValue *)valueWithSize:(NSSize)size;

+ (NSValue *)valueWithRect:(NSRect)rect;

- (NSPoint)pointValue;

- (NSSize)sizeValue;

- (NSRect)rectValue;

 

 

复制代码
  //声明并初始化一个struct
    NSRect rtc = NSMakeRect(12, 32, 433, 343);
    //创建一个NSValue:
    //value:对象地址
    //objCType:通常是一个用来描述对象类型和大小的字符串,@encode会自动生成一个合适的描述字符串
    NSValue *value = [NSValue valueWithBytes:&rtc objCType:@encode(NSRect)];
    //把value添加到数组中
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
    [array addObject:value];
    
    NSRect rt;
    //从数组中取到NSValue,因为只添加了一个,所以小标是0
    NSValue *v = [array objectAtIndex:0];
    //从value中取得一个NSRect,虽然返回值是void,但其实是它是利用指针返回值的
    [v getValue:&rt];
    //输出结果
    NSLog(@"%f %f %f %f", rt.origin.x, rt.origin.y, rt.size.height, rt.size.width);
    
    //用快速枚举遍历array并输出NSValue中struct属性的值
    for(NSValue *v in array)
    {
        NSRect rt;
        [v getValue:&rt];
        NSLog(@"%f %f %f %f", rt.origin.x, rt.origin.y, rt.size.height, rt.size.width);
    }
    
    
    /////////////////////便捷的struct转换成NAValue方式////////////////////////
    //声明并初始化一个struct
    NSRect rtc1 = NSMakeRect(100, 200, 300, 400);
    //创建一个NSValue
    NSValue *value1 = [NSValue valueWithRect:rtc1];
    //把value1添加到数组中
    NSMutableArray *array1 = [NSMutableArray arrayWithCapacity:10];
    [array1 addObject:value1];
    NSRect rt1 = [value1 rectValue];
    //输出结果
    NSLog(@"%f %f %f %f", rt1.origin.x, rt1.origin.y, rt1.size.height, rt1.size.width);
复制代码

 

NSNull

在集合中不能存放nil值,因为在NSArray和NSDictionary中nil有特殊的含义。但是在有些时候,确实需要用到这样的空值,比如在字典中,电话簿中"Jack"关键字下有电话号码、家庭住址、Email等等信息,但是现在只知道他的电话号码,这种不知道其他信息的情况下为了消除一些歧义,有必要将它们设置为空,所以Cocoa提供了NSNull

NSNull只有一个方法:null

[dictionary setObject:[NSNull null], forKey:"Email"];

if(EmailAdress == [NSNull null])

{

  //to do something...

}

抱歉!评论已关闭.