#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
/*
OC中封装、拆包基本数据类型
Foundation框架下的基础类
NSValue / NSNumber
功能:将OC和C 当中的基本数据类型转换成实例对象,即将'值类型'转换成'引用类型'
*/
//OC中常用的数据类型
int a = 5;
float b = 5.0;
double c = 5.5 ;
char d = 'a';
bool flag = YES;
// CGPoint point = {3,4};
// CGSize size = {5,6};
// CGRect rect = {point,size};
//NSRange包含了两个成员,location 以及 length。location表示起始位置,以0开始;length表示长度
// NSRange range = {10,3};
CGPoint point = CGPointMake(3, 4);
CGSize size = CGSizeMake(5, 6);
CGRect rect = CGRectMake(3, 4, 5, 6);
NSRange range = NSMakeRange(10, 3);
NSEdgeInsets edgeInsets = NSEdgeInsetsMake(10, 10, 10, 10);
//NSValue 以及 NSNumber
//NSValue 为 NSNumber的父类
//其中NSValue 可以将CGPoint,CGSize,CGRect,NSRange,NSEdgeInsets转换成对象
NSValue *pointVa = [NSValue valueWithPoint:point];
NSValue *sizeVa = [NSValue valueWithSize:size];
NSValue *rectVa = [NSValue valueWithRect:rect];
NSValue *rangeVa = [NSValue valueWithRange:range];
NSValue *edgeInsetsVa = [NSValue valueWithEdgeInsets:edgeInsets];
//其中NSNumber 可以将'int','float','double','char','bool'等C中基本数据类型转换成对象
NSNumber *intNumber = [NSNumber numberWithInt:a];
NSNumber *floatNumber = [NSNumber numberWithFloat:b];
NSNumber *doubleNumber = [NSNumber numberWithDouble:c];
NSNumber *charNumber = [NSNumber numberWithChar:d];
NSNumber *boolNumber = [NSNumber numberWithBool:flag];
//我们将C和OC中的基本数据类型(值类型)转换成对象(引用类型)的过程,叫做封装。相对应的也有一个将对象转变成基本数据类型,此过程叫做拆包。
//拆包
int a1 = [intNumber intValue];
float b1 = [floatNumber floatValue];
double c1 = [doubleNumber doubleValue];
char d1 = [charNumber charValue];
bool flag1 =[boolNumber boolValue];
CGPoint point1 = [pointVa pointValue];
CGSize size1 = [sizeVa sizeValue];
CGRect rect1 = [rectVa rectValue];
NSRange range1 = [rangeVa rangeValue];
NSEdgeInsets edgeInsets1 = [edgeInsetsVa edgeInsetsValue];
}
return 0;
}