在OC中NSArray (数组)、NSDictionary(字典)和NSSet(集合)它们存放的都是对象类型,要想把基本数据类型和结构体类型存放到NSArray、NSDictionary或NSSet中就需要对它们进行封装。
//基本数据类型等
int a= 5;float b = 4.56;
double c =34.234;
char d = 'c';
long e = 233;
BOOL f = YES;
NSInteger r = 132;
//结构体类型
CGPoint point = CGPointMake(2, 3);
CGRect rect= CGRectMake(0, 0, 34, 5);
CGSize size = CGSizeMake(4, 4);
NSRange range= NSMakeRange(5, 5);
NSEdgeInsets edgeInsets = NSEdgeInsetsMake(1, 1, 5, 5);
//封装, NSValue 是 NSNumber 的父类
//NSValue可以讲结构体转化成对象,NSNumber可以将基本数据类型转化成对象,这些过程叫做封装。
NSNumber *aNumber = [NSNumber numberWithInt:a];
NSNumber *bNumber = [NSNumber numberWithFloat:b];
NSNumber *cNumber = [NSNumber numberWithDouble:c];
NSNumber *dNUmber = [NSNumber numberWithChar:d];
NSNumber *eNumber = [NSNumber numberWithLong:e];
NSNumber *fNumber = [NSNumber numberWithBool:f];
NSNumber *rNumber = [NSNumber numberWithInteger:r];
NSValue *pointValue = [NSValue valueWithPoint:point];
NSValue *rectValue = [NSValue valueWithRect:rect];
NSValue *rangeValue = [NSValue valueWithRange:range];
NSValue *sizeValue = [NSValue valueWithSize:size];
NSValue *edgeInsetsValue = [NSValue valueWithEdgeInsets:edgeInsets];
//将对象转变成基本数据类型,这叫拆包
int a1= [aNumber intValue];
BOOL f1 = [fNumber boolValue];
NSInteger r1 = [rNumber integerValue];
CGPoint point1 = [pointValue pointValue];
CGRect rect1 = [rectValue rectValue];
NSRange range1 = [rangeValue rangeValue];
//将nil封装
id nullValue = [NSNull null];