NSValue

NSValue
是NSNumber的父类
结构体 指针 复合类型 使用NSValue来封装 int a[10]也可以来封装
封装 上面的结构体

NSValue * value = [NSValue valueWithPoint:p];
NSValue *value1 = [NSValue valueWithSize:s];
NSValue *value2 = [NSValue valueWithRect:r];
NSValue * range1 = [NSValue valueWithRange:range];
//取值
CGPoint p1 = [value pointValue];
CGSize s1 = [value1 sizeValue];
CGRect r1 = [value rectValue];
NSRange rang2 = [range1 rangeValue];

//自定义了一个结构体
struct Student
{
int age;
};
struct Student a ={10};
//把自定义的 结构体封装
//第一个参数 是放要封装的这个东西的地址
//第二个放的是 封装时候编码的类型
NSValue * valueM = [[NSValue alloc]initWithBytes:&a objCType:@encode(struct Student)];
//取出来
struct Student newStudent;
[valueM getValue:&newStudent];

    //自定义C数组
    int b[10] = {1,2,3,4,5,6,7,8,9,10};
    NSValue * valueB = [[NSValue alloc]initWithBytes:b objCType:@encode(int[10])];
    int c[20];
    [valueB getValue:c];

CGPoint p = CGPointMake(20, 20);
CGSize s = CGSizeMake(30, 30);
CGRect r = CGRectMake(200, 200, 200, 200);
NSRange range = NSMakeRange(10, 20);
//放到数组去
NSValue * value = [NSValue valueWithPoint:p];
NSValue *value1 = [NSValue valueWithSize:s];
NSValue *value2 = [NSValue valueWithRect:r];
NSValue * range1 = [NSValue valueWithRange:range];
//NSArray *arr = @[value];
//取值
CGPoint p1 = [value pointValue];
CGSize s1 = [value1 sizeValue];
CGRect r1 = [value rectValue];
NSRange rang2 = [range1 rangeValue];
//自定义了一个结构体
struct Student
{
int age;
};
struct Student a ={10};
//把自定义的 结构体封装
//第一个参数 是放要封装的这个东西的地址
//第二个放的是 封装时候编码的类型
NSValue * valueM = [[NSValue alloc]initWithBytes:&a objCType:@encode(struct Student)];
//取出来
struct Student newStudent;
[valueM getValue:&newStudent];

    //自定义C数组
    int b[10] = {1,2,3,4,5,6,7,8,9,10};
    NSValue * valueB = [[NSValue alloc]initWithBytes:b objCType:@encode(int[10])];
    int c[20];
    [valueB getValue:c];

你可能感兴趣的:(NSValue)