[OC Foundation框架 - 13] NSValue
NSNumber能够包装基本数据类型称为OC对象,是因为继承了NSValue
包装结构体成OC对象
1.自带结构体
1 void value() 2 { 3 CGPoint point = CGPointMake(10, 10); 4 NSValue *obj = [NSValue valueWithPoint:point]; 5 NSLog(@"%@", obj); 6 7 CGPoint p1 = [obj pointValue]; 8 BOOL result = CGPointEqualToPoint(point, p1); 9 NSLog(@"result = %i", result); 10 }
2.自定义结构体
1 typedef struct 2 { 3 int year; 4 int month; 5 int day; 6 } Date; 7 8 void value2() 9 { 10 Date d1 = {2012,12,12}; 11 //void * 代表任何指针 12 13 //根据结构体类型生成对应的类型描述字符串 14 char *type = @encode(Date); 15 NSValue *v1 = [NSValue value:&d1 withObjCType:type]; 16 17 Date d2; 18 [v1 getValue:&d2]; 19 NSLog(@"%i-%i-%i", d1.year, d1.month, d1.day); 20 }