/*--------------------NSNumber--------------------*/ //包装基本数据类型 //1.创建number对象 //12,1.9,‘A‘,YES //整型 //(1)包装 NSNumber *intNum = [NSNumber numberWithInteger:12]; NSNumber *floatNum = [NSNumber numberWithDouble:1.9]; NSNumber *charNum = [NSNumber numberWithChar:‘A‘]; NSNumber *boolNum = [NSNumber numberWithBool:YES]; NSArray *array = @[intNum,floatNum,charNum,boolNum]; NSLog(@"%@",array); //(2)解包 NSInteger intValue = [intNum integerValue]; CGFloat floatValue = [floatNum doubleValue]; char c = [charNum charValue]; bool b = [boolNum boolValue]; //优化语法 NSNumber *intNum2 = @12; NSNumber *floatNum2 = @1.9; NSNumber *charNum2 = @‘A‘; NSNumber *boolNum2 = @YES; NSInteger count = 12; NSNumber *num = @(count); /*--------------------NSValue--------------------*/ //将结构体变成对象 NSRange range = {1,2}; NSValue *rgValue = [NSValue valueWithRange:range]; NSLog(@"range:%@",rgValue); // [NSValue valueWithSize:/] //自定义的结构体 struct CXPoint { CGFloat x; CGFloat y; }; //----对自定义的结构体封装---- struct CXPoint p = {1.2,9.8}; NSValue *pValue = [NSValue value:&p withObjCType:@encode(struct CXPoint)]; NSLog(@"%@",pValue); //----对自定义的结构体解封装---- struct CXPoint p2; [pValue getValue:&p2]; NSLog(@"%f %f",p2.x,p2.y); /*---------------------NSNull---------------------*/ //nil不能作为元素存放 // NSArray *array2 = @[@1,@2,@3,nil,@5]; NSNull *null = [NSNull null]; NSArray *array2 = @[@1,@2,@3,null,@5]; NSLog(@"array2:%@",array2);
时间: 2024-10-10 20:16:28