NSValue可以包装任意值,并且NSValue可以放在NSArray和NSDicionary中
+ (NSValue *)valueWithBytes:(const void *)value objCType:(const char *)type;
将值放入NSValue
+ (NSValue *)value:(const void *)value withObjCType:(const char *)type;
将值从NSValue中取出sample code:
NSRect rect=NSMakeRect(12, 21, 40, 40);
NSValue *value=[NSValue valueWithBytes:&rect objCType:@encode(NSRect)];
NSMutableArray *arr=[NSMutableArray arrayWithObjects:value, nil];
NSLog(@"the arr is %@",arr);
value=[arr objectAtIndex:0];
NSLog(@"the value is %@",value);
[value getValue:&rect];
结果:
2013-11-15 11:39:38.311 elementFirst[538:303] the arr is (
"NSRect: {{12, 21}, {40, 40}}"
)
2013-11-15 11:39:38.313 elementFirst[538:303] the value is NSRect: {{12, 21}, {40, 40}}