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