NSNumber
- NSNumber 是一个类。OC中提供了NSNumber类来将基本数据类型“包装”成数字对象,这样我们就可以将基本数据类型当做对象来处理。同样也可以将包装后的对象还原成基本数据类型。
NSNumber的类方法
+ (NSNumber *)numberWithChar:(char)value;
+ (NSNumber *)numberWithUnsignedChar:(unsigned char)value;
+ (NSNumber *)numberWithShort:(short)value;
+ (NSNumber *)numberWithUnsignedShort:(unsigned short)value;
+ (NSNumber *)numberWithInt:(int)value;
+ (NSNumber *)numberWithUnsignedInt:(unsigned int)value;
+ (NSNumber *)numberWithLong:(long)value;
+ (NSNumber *)numberWithUnsignedLong:(unsigned long)value;
+ (NSNumber *)numberWithLongLong:(long long)value;
+ (NSNumber *)numberWithUnsignedLongLong:(unsigned long long)value;
+ (NSNumber *)numberWithFloat:(float)value;
+ (NSNumber *)numberWithDouble:(double)value;
+ (NSNumber *)numberWithBool:(BOOL)value;
+ (NSNumber *)numberWithInteger:(NSInteger)value NS_AVAILABLE(10_5, 2_0);
+ (NSNumber *)numberWithUnsignedInteger:(NSUInteger)value NS_AVAILABLE(10_5, 2_0);
NSNumber的实例方法
//实例方法 初始化
- (NSNumber *)initWithChar:(char)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithUnsignedChar:(unsigned char)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithShort:(short)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithUnsignedShort:(unsigned short)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithInt:(int)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithUnsignedInt:(unsigned int)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithLong:(long)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithUnsignedLong:(unsigned long)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithLongLong:(long long)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithUnsignedLongLong:(unsigned long long)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithFloat:(float)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithDouble:(double)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithBool:(BOOL)value NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithInteger:(NSInteger)value NS_AVAILABLE(10_5, 2_0) NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithUnsignedInteger:(NSUInteger)value NS_AVAILABLE(10_5, 2_0) NS_DESIGNATED_INITIALIZER;
- 使用 @(someThing);// 快速装箱,自动判断类型。
NSNumber *number = @(1);
将基本类型数据封装到NSNumber中后,通过下面的实例方法重新获取它
NSNumber *myNumber = [NSNumber numberWithLong:0xabcdef];
NSNumber *floatnumber = [NSNumber numberWithFloat:12.33];
NSNumber *intNumber = [NSNumber numberWithInt:100];
NSNumber *doublenumber = [NSNumber numberWithDouble:12345e+15];
NSNumber *charNumber = [NSNumber numberWithChar:'X'];
int myInt = [intNumber intValue];
float myFloat = [floatnumber floatValue];
double myDouble = [doublenumber doubleValue];
char myChar = [charNumber charValue];
- 因为OC的集合当中存储的数据必须是OC对象。举个例子
NSMutableArray *myArray = [[NSMutableArray alloc] init ];
[myArray addObject: 6 ];
上面的写法编译器会报错。解决方法就是把'6'包装成一个NSNumber对象。
NSMutableArray *myArray = [[NSMutableArray alloc] init ];
[myArray addObject:[NSNumber numberWithInt:6] ];
NSLog(@"array content is %@", myArray);
- NSNumber转成NSString类型
// 下面 A是NSNumber类型 B是NSString类型
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
B = [numberFormatter stringFromNumber:A];
- 用NSNumber排序
NSNumber *number1 = [NSNumber numberWithInteger:13];
NSNumber *number2 = [NSNumber numberWithInteger:14];
NSComparisonResult result = [number1 compare:number2];
if (result == NSOrderedAscending) {
NSLog(@"升序");//number2 比 number1 大
}else if (result == NSOrderedSame){
NSLog(@"一致");
}else if (result == NSOrderedDescending){
NSLog(@"降序");
}else{
NSLog(@"error");
}
- 用NSNumber判断是否一致
NSNumber *number1 = [NSNumber numberWithInteger:13];
NSNumber *number2 = [NSNumber numberWithInteger:14];
if ([number1 isEqualToNumber:number2]) {
NSLog(@"same");
}else{
NSLog(@"nonono");
}
int、NSInteger、NSUInteger、NSNumber之间的区别和联系
- int : 当使用int类型定义变量的时候,可以像写C程序一样去使用。当你不知道程序运行在哪种处理器架构时,你最好使用NSInteger,因为在有可能int在32位系统中只是int类型,而在64位系统,int可能变成long型。除非不得不使用int/long型,否则推荐使用NSInteger。
- NSInteger/NSUInteger是一种动态定义的类型,在不同的设备,不同的架构,有可能是int类型,有可能是long类型。NSUInteger是无符号的,即没有负数,NSInteger是有符号的。
- NSInteger是基础类型,NSNumber是一个类,如果需要存储一个数值,直接使用NSInteger是不行的,因为OC的集合当中存储的数据必须是OC对象。