基本,集合,複雜,對象
可用對象封裝基本數值,然後將對象放入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;
//聲明並初始化一個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);
自定義類型
typedef struct
{
int id;
float height;
unsigned char flag;
} MyTestStruct;
MyTestStruct myTestSturct;
myTestSturct.id = 1;
myTestSturct.height = 23.0;
myTestSturct.flag = 'A';
//封裝數據
NSValue *value = [NSValue valueWithBytes:&myTestSturct objCType:@encode(MyTestStruct)];
//取出value中的數據
MyTestStruct theTestStruct;
[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...
}
//聲明並初始化一個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); }
//函數聲明,定義時,需要出現參數類型
//調用函數時,不出現數據類型 /1
//若出現數據類型,則爲強制轉換 /2
NSMutableArray *kbArr;
NSMutableDictionary *kbDic;
NSRect stRect = {1,20,200,200};
//NSValue *vlaue = [NSValue valueWithRect:stRect];
//1
NSValue *vlaue = [NSValue valueWithBytes:&stRect objCType:@encode(NSRect)];
//2
NSValue *vlaue1 = [NSValue valueWithBytes:(const void *)&stRect objCType:(const char *)@encode(NSRect)];
[kbArr addObject:vlaue];
[kbDic setObject:vlaue forKey:@"stRect"];
本章總結:
掌握nsnumber類的作用,實用方法。
掌握nsvalue類的作用,實用方法。
掌握nsnull類的作用,實用方法。