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