1 NSvalue把复杂的数据类型(指针、结构体等)存储成对象 2 3 struct sct{ 4 int a; 5 int b; 6 }sctt={4,5}; 7 8 NSValue *value=[[NSValue alloc] initWithBytes:&sctt objCType:@encode(struct sct)]; 9 10 NSLog(@"%s",@encode(int));结果是i 11 NSLog(@"%s",@encode(float));结果是f 12 NSLog(@"%s",@encode(long));结果是q 13 NSLog(@"%s",@encode(struct sct));结果是{sct=ii} 14 //同过每一个类型生成的字符串是唯一的 15 if(strcmp(value.objCType, @encode(struct sct))==0{ 16 NSLog(@"it is struct sct"); 17 } 18 19 //取出结构体数据 20 struct sct newSctt; 21 [value getValue:&newSctt]; 22 NSLog(@"%d %d",newSctt.a,newSctt.b);//结果是 4 5 23 [value release]; 24 25 26 27 char *p=(char *)0x1f; 28 NSValue *value1=[[NSValue alloc] initWithBytes:&p objCType:@encode(char *)]; 29 30 char *q; 31 [value1 getValue:&q]; 32 NSLog(@"%p",q);结果是0x1f 33 [value1 release]; 34 35 36 CGPoint point; 37 CGsize size; 38 CGRect rect; 39 NSValue *pointValue=[NSValue valueWithPoint:point]; 40 NSValue *sizeValue=[NSValue valueWithSize:size]; 41 NSvalue *recrValue=[NSValue valueWithRect:rect]; 42 NSValue & rangeValue=[NSValue valueWithRange:NSMakeRange(3,4)];