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

结题!!!

原文地址:https://www.cnblogs.com/GISerYang/p/3345292.html