數值類對象:NSNumber,NSValue,NSNull

基本,集合,複雜,對象

可用對象封裝基本數值,然後將對象放入NSArray或NSDictionary 中。

用對象封裝基本數值後,即可給其發送消息。

數值類型包括:NSNumber,NSValue,NSNull類。


oc不支持自動裝箱


NSNumber類

創建方法

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

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

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

+ (NSNumber *)numberWithDouble:(double)value;

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

+ (NSNumber *)numberWithInteger:(NSInteger)valueNS_AVAILABLE(10_5, 2_0);


創建類型後,就可以放入字典或數組中。

解除裝箱,獲取數據:


- (char)charValue;

- (int)intValue;

- (float)floatValue;

- (double)doubleValue;

- (BOOL)boolValue;

可以任意類型的轉換


發送消息:

NSNumber 繼承自NSObject;

可使用compare、isEqual等消息。


NSNumber是NSValue的子類。

NSValue可包裝任意類型值。


泛型指針

void * 針對簡單,複雜數據類型

id     可指向任意oc對象的指針


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(出參);

從接受value的對象中

提取數值

提取的數值,存放在這個指針所指向的內存塊裏。

 

3、Cocoa提供了常用struct數據類型轉換成NSValue的便捷方法:

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

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

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

- (NSPoint)pointValue;

- (NSSize)sizeValue;

- (NSRect)rectValue;

  1. //聲明並初始化一個struct  
  2. NSRect rtc = NSMakeRect(12, 32, 433, 343);  
  3. //創建一個NSValue:  
  4. //value:對象地址  
  5. //objCType:通常是一個用來描述對象類型和大小的字符串,@encode會自動生成一個合適的描述字符串  
  6. NSValue *value = [NSValue valueWithBytes:&rtc objCType:@encode(NSRect)];  
  7. //把value添加到數組中  
  8. NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];  
  9. [array addObject:value];  
  10.   
  11. NSRect rt;  
  12. //從數組中取到NSValue,因爲只添加了一個,所以小標是0  
  13. NSValue *v = [array objectAtIndex:0];  
  14. //從value中取得一個NSRect,雖然返回值是void,但其實是它是利用指針返回值的  
  15. [v getValue:&rt];  
  16. //輸出結果  
  17. NSLog(@"%f %f %f %f", rt.origin.x, rt.origin.y, rt.size.height, rt.size.width);  
  18.   
  19. //用快速枚舉遍歷array並輸出NSValue中struct屬性的值  
  20. for(NSValue *v in array)  
  21. {  
  22.     NSRect rt;  
  23.     [v getValue:&rt];  
  24.     NSLog(@"%f %f %f %f", rt.origin.x, rt.origin.y, rt.size.height, rt.size.width);  
  25. }  
  26.   
  27.   
  28. /////////////////////便捷的struct轉換成NAValue方式////////////////////////  
  29. //聲明並初始化一個struct  
  30. NSRect rtc1 = NSMakeRect(100, 200, 300, 400);  
  31. //創建一個NSValue  
  32. NSValue *value1 = [NSValue valueWithRect:rtc1];  
  33. //把value1添加到數組中  
  34. NSMutableArray *array1 = [NSMutableArray arrayWithCapacity:10];  
  35. [array1 addObject:value1];  
  36. NSRect rt1 = [value1 rectValue];  
  37. //輸出結果  
  38. NSLog(@"%f %f %f %f", rt1.origin.x, rt1.origin.y, rt1.size.height, rt1.size.width);  

自定義類型

  1. typedef struct  
  2. {  
  3.     int    id;  
  4.     float  height;  
  5.     unsigned char   flag;  
  6. } MyTestStruct;  
  7.   
  8. MyTestStruct myTestSturct;  
  9. myTestSturct.id = 1;  
  10. myTestSturct.height = 23.0;  
  11. myTestSturct.flag = 'A';  
  12. //封裝數據  
  13. NSValue *value = [NSValue valueWithBytes:&myTestSturct objCType:@encode(MyTestStruct)];  
  14. //取出value中的數據  
  15. MyTestStruct  theTestStruct;  
  16.   
  17. [value getValue:&theTestStruct];//可對theTestStruct操作取得其中的數據  


NSNull

在集合中不能存放nil值,因爲在NSArray和NSDictionary中nil有特殊的含義。但是在有些時候,確實需要用到這樣的空值,比如在字典中,電話簿中"Jack"關鍵字下有電話號碼、家庭住址、Email等等信息,但是現在只知道他的電話號碼,這種不知道其他信息的情況下爲了消除一些歧義,有必要將它們設置爲空,所以Cocoa提供了NSNull

NSNull只有一個方法:null

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

if(EmailAdress == [NSNull null])

{

  //to do something...

}

  1. //聲明並初始化一個struct    
  2.     NSRect rtc = NSMakeRect(12, 32, 433, 343);    
  3.     //創建一個NSValue:    
  4.     //value:對象地址    
  5.     //objCType:通常是一個用來描述對象類型和大小的字符串,@encode會自動生成一個合適的描述字符串    
  6.     NSValue *value = [NSValue valueWithBytes:&rtc objCType:@encode(NSRect)];    
  7.     //把value添加到數組中    
  8.     NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];    
  9.     [array addObject:value];    
  10.       
  11.     NSRect rt;    
  12.     //從數組中取到NSValue,因爲只添加了一個,所以小標是0    
  13.     NSValue *v = [array objectAtIndex:0];    
  14.     //從value中取得一個NSRect,雖然返回值是void,但其實是它是利用指針返回值的    
  15.     [v getValue:&rt];    
  16.     //輸出結果    
  17.     NSLog(@"%f %f %f %f", rt.origin.x, rt.origin.y, rt.size.height, rt.size.width);    
  18.       
  19.     //用快速枚舉遍歷array並輸出NSValue中struct屬性的值    
  20.     for(NSValue *v in array)    
  21.     {    
  22.         NSRect rt;    
  23.         [v getValue:&rt];    
  24.         NSLog(@"%f %f %f %f", rt.origin.x, rt.origin.y, rt.size.height, rt.size.width);    
  25.     }    
  26.       
  27.       
  28.     /////////////////////便捷的struct轉換成NAValue方式////////////////////////    
  29.     //聲明並初始化一個struct    
  30.     NSRect rtc1 = NSMakeRect(100, 200, 300, 400);    
  31.     //創建一個NSValue    
  32.     NSValue *value1 = [NSValue valueWithRect:rtc1];    
  33.     //把value1添加到數組中    
  34.     NSMutableArray *array1 = [NSMutableArray arrayWithCapacity:10];    
  35.     [array1 addObject:value1];    
  36.     NSRect rt1 = [value1 rectValue];    
  37.     //輸出結果    
  38.     NSLog(@"%f %f %f %f", rt1.origin.x, rt1.origin.y, rt1.size.height, rt1.size.width);  }  

  1. //函數聲明,定義時,需要出現參數類型  
  2.    //調用函數時,不出現數據類型      /1  
  3.    //若出現數據類型,則爲強制轉換    /2  
  4.    NSMutableArray *kbArr;  
  5.    NSMutableDictionary *kbDic;  
  6.      
  7.    NSRect stRect = {1,20,200,200};  
  8.    //NSValue *vlaue = [NSValue valueWithRect:stRect];  
  9.      
  10.    //1  
  11.    NSValue *vlaue = [NSValue valueWithBytes:&stRect objCType:@encode(NSRect)];  
  12.    //2  
  13.    NSValue *vlaue1 = [NSValue valueWithBytes:(const void *)&stRect objCType:(const char *)@encode(NSRect)];  
  14.      
  15.    [kbArr addObject:vlaue];  
  16.    [kbDic setObject:vlaue forKey:@"stRect"];  



本章總結:


掌握nsnumber類的作用,實用方法。

掌握nsvalue類的作用,實用方法。

掌握nsnull類的作用,實用方法。

發佈了104 篇原創文章 · 獲贊 9 · 訪問量 37萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章