NSValue的用法

来源:互联网 发布:p图 淘宝刷手 编辑:程序博客网 时间:2024/05/20 00:36

NSValue可以包装任意值,并且NSValue可以放在NSArray和NSDicionary中     

+ (NSValue *)valueWithBytes:(constvoid *)value objCType:(const char *)type;

将值放入NSValue

+ (NSValue *)value:(constvoid *)value withObjCType:(const char *)type;

将值从NSValue中取出

sample code:


        NSRect rect=NSMakeRect(12,21, 40, 40);

       NSValue *value=[NSValue valueWithBytes:&rectobjCType:@encode(NSRect)];

       NSMutableArray *arr=[NSMutableArrayarrayWithObjects:value, nil];

       NSLog(@"the arr is %@",arr);

        

        value=[arrobjectAtIndex:0];

       NSLog(@"the value is %@",value);

        

        [valuegetValue:&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}}


原创粉丝点击