OC基礎數據類型-NSValue


1、NSValue:將指針等復雜的類型存儲為對象

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中結構體的數據

 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);

 

結題!!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM