OC基礎數據類型-NSValue

一、NSValue:將指針等複雜的類型存儲爲對象spa

1 struct sct {
2     int a;
3     int b;
4 }sctt;
1 NSValue * value = [[NSValue alloc] initWithBytes:&sctt objCType:@encode(struct sct)];

判斷NSValue存儲的類型指針

1 if(strcmp(value.objCType, @encode(struct sct)) == 0){
2     NSLog(@"It is struct sct");
3 }

獲取NSValue中結構體的數據code

 1 //初始化sct的a、b
 2 struct sct {
 3     int a;
 4     int b;
 5 }sctt = {4, 5};
 6 
 7 //獲取NSValue中結構體的數據
 8 struct sct newSct;
 9 [value getValue:&newSct];
10 NSLog(@"%d, %d", newSct.a, newSct.b);
1 char * p = (char *)0x1f;
2 NSValue * value = [[NSValue alloc] initWithBytes:&p objCType:@encode(char *)];
3 
4 char * q;
5 [value getValue:&q];
6 NSLog(@"%p", q);

 

結題!!!對象

相關文章
相關標籤/搜索