Foundaton框架-->NSValue

2016-10-22  本文已影响43人  爱貌貌Manners

一、NSValue的基本概念

二、NSValue的常见的包装

CGPoint point = NSMakePoint(11,22);
NSValue *value = [NSValue valueWithPoint:point];
NSArray *arr = @[value];

三、任意数据的包装

 typedef struct{
        int age;
        char *name;
        double height;
    }Person;
    Person p = {30, "lnj", 1.75};
    NSValue *pValue = [NSValue valueWithBytes:&p objCType:@encode(Person)];
    NSArray *arr = @[pValue];
    NSLog(@"%@", arr);
    // 从NSValue中取出自定义的结构体变量
    Person res;
    [pValue getValue:&res];
    NSLog(@"age = %i, name = %s, height = %f", res.age, res.name, res.height);
    // 从NSValue中取出自定义的结构体变量
    Person res;
    [pValue getValue:&res];
    NSLog(@"age = %i, name = %s, height = %f", res.age, res.name, res.height);
上一篇 下一篇

猜你喜欢

热点阅读