ios NSNumber详解
NSArray、NSDictionary、NSSet 等集合对象只能保存对象,如果要保存 char、short、int、float、double、BOOL 或指向结构的指针等基础数据类型,则可以先将这些基本数据类型封装成 NSNumber 对象,再存入集合对象。
NSNumber 类用来包装基本数据类型。
NSValue 是 NSNumber 的父类。
不能直接用 NSNumber 对象做计算,只能提取值。
一般用法:
// 包装基本数据类型
NSNumber *intNumber = [NSNumber numberWithInteger:100];
NSNumber *floatNumber =[NSNumber numberWithFloat:24.58];
NSNumber *boolNumber = [NSNumber numberWithBool:YES];
// 字面量语法
NSNumber *intNum = @1;
NSNumber *floatNum = @2.5f;
NSNumber *doubleNum = @3.14159;
NSNumber *boolNum = @YES;
NSNumber *charNum = @'a';
int x = 5;
float y = 6.32f;
NSNumber *expressionNum = @(x * y);
// 封装完后存入数组
NSArray *array = @[intNumber,floatNumber,boolNumber];
// 解包
NSInteger intValue = [intNumber integerValue];
float floatValue = [floatNumber floatValue];
详细示例
可封装的数据类型如下:
- (NSNumber *)initWithChar:(char)value;
- (NSNumber *)initWithUnsignedChar:(unsigned char)value;
- (NSNumber *)initWithShort:(short)value;
- (NSNumber *)initWithUnsignedShort:(unsigned short)value;
- (NSNumber *)initWithInt:(int)value;
- (NSNumber *)initWithUnsignedInt:(unsigned int)value;
- (NSNumber *)initWithLong:(long)value;
- (NSNumber *)initWithUnsignedLong:(unsigned long)value;
- (NSNumber *)initWithLongLong:(long long)value;
- (NSNumber *)initWithUnsignedLongLong:(unsigned long long)value;
- (NSNumber *)initWithFloat:(float)value;
- (NSNumber *)initWithDouble:(double)value;
- (NSNumber *)initWithBool:(BOOL)value;
- (NSNumber *)initWithInteger:(NSInteger)value;
- (NSNumber *)initWithUnsignedInteger:(NSUInteger)value;
使用示例:
// char 类型
NSNumber *charNumber = [NSNumber numberWithChar:'X'];
NSLog(@"%c",[charNumber charValue]); // X
NSLog(@"%@ %@",NSStringFromClass(charNumber.class),@(charNumber.objCType));
// __NSCFNumber c
// unsigned char 类型
NSNumber *unsignedCharNumber = [NSNumber numberWithUnsignedChar:'a'];
NSLog(@"%c",[unsignedCharNumber unsignedCharValue]); // a
NSLog(@"%@ %@",NSStringFromClass(unsignedCharNumber.class),@(unsignedCharNumber.objCType));
// __NSCFNumber s
// short
NSNumber *shortNumber = [NSNumber numberWithShort:50];
NSLog(@"%hi",[shortNumber shortValue]); // 50
NSLog(@"%@ %@",NSStringFromClass(shortNumber.class),@(shortNumber.objCType));
// __NSCFNumber s
// unsigned short
NSNumber *unsignedshortNumber = [NSNumber numberWithUnsignedShort:50];
NSLog(@"%hu",[unsignedshortNumber unsignedShortValue]); // 50
NSLog(@"%@ %@",NSStringFromClass(unsignedshortNumber.class),@(unsignedshortNumber.objCType));
// __NSCFNumber i
// int 类型
NSNumber *intNumber = [NSNumber numberWithInt:100];
NSLog(@"%i",[intNumber intValue]); // 100
NSLog(@"%@ %@",NSStringFromClass(intNumber.class),@(intNumber.objCType));
// __NSCFNumber i
// unsigned int 类型
NSNumber *unsignedIntNumber = [NSNumber numberWithUnsignedInt:100];
NSLog(@"%u",[unsignedIntNumber unsignedIntValue]); // 100
NSLog(@"%@ %@",NSStringFromClass(unsignedIntNumber.class),@(unsignedIntNumber.objCType)); // __NSCFNumber q
// long 类型
NSNumber *longNumber = [NSNumber numberWithLong:0xabcdef];
NSLog(@"%lx",[longNumber longValue]); // abcdef
NSLog(@"%@ %@",NSStringFromClass(longNumber.class),@(longNumber.objCType));
// __NSCFNumber q
// unsigned long 类型
NSNumber *unsignedLongNumber = [NSNumber numberWithUnsignedLong:0xabcdef];
NSLog(