NSValue:(只能存放那些没有指针变量的结构,不然就只有指针地址被编码,而数据没有被编码)
是一个存储C/C++或者Obj-C数据的容器类。它可以保存任意类型的数据,比如C/C++标准的int,float,char等,也可以是指针、结构,以及Obj-C的对象。
NSValue类的目标就是允许以上数据类型的数据结构能够被添加到集合里。
使用NSValue要注意:不可以存储C字符串、不定长数组/结构和其他一些不定长的数据类型到NSValue中,你应该使用NSString或者NSData来存储此类不定长数据。当然你可以把一个指向变长对象的指针存储在NSValue对象中。
typedef struct{
char value1;
char value2;
int value3;
}NSValueTest;
NSValueTest user={@"张三",@"hehe",2};
NSValue *userValue=[NSValue valueWithBytes:&user objCType:@encode(User)];
NSArray *myArray=[NSArray arrayWithObjects:@"h1",userValue,nil];
userValue=[myArray objectAtIndex:1];
[userValue getValue:&user];
NSLog(@"用户姓名: %@",user.name);
NSDictionary:
1. 将 infoStruct转换为NSData
NSData * msgData = [[NSData alloc]initWithBytes:&infoStruct length:sizeof(infoStruct)];
2. 将 msgData转换为 MYINFO 对象。
struct MYINFO infoStruct2;
[msgData getBytes:&infoStruct2 length:sizeof(infoStruct2)];
http://www.tekuba.net/program/276/
http://www.cnblogs.com/pengyingh/articles/2371398.html