数字对象NSNumber的使用

先简述下关于NSNumber的信息

  • NSNumber的存在就相当于java中的装箱与拆箱。只不过java中的装箱拆箱过程,使用的是对应的类型,比如基本数据类型是int、double类型,装箱时就得对应使用Integer、Double类型。而Objective-C中,使用的都是NSNumer类型。也因此NSNumber其实是一个类簇,而不是一个类。
  • 在Objective-C编程中,常常是需要将基本数据类型转换为对象来使用的。比如,NSArray、NSDictionary中只能放id类型(即对象类型)。服务器返回的JSON数据解析后是包含的是NSNumber而不是基本数据类型。
  • NSNumber是继承于NSValue。
  • 正是因为NSNumber是一个类簇,会导致使用NSNumber装箱BOOL类型,后面却拆箱成NSInteger类型。虽然基本数据类型之间是有一套相互转换的机制的,但是为了方便省事,建议就不要进行骚操作了吧。

接下来主要就是细致化的把对应的封装、拆箱的方法罗列出来。

一、创建一个NSNumber对象(装箱)

// char
+ (NSNumber *)numberWithChar:(char)value;

// 无符号char
+ (NSNumber *)numberWithUnsignedChar:(unsigned char)value;

// short
+ (NSNumber *)numberWithShort:(short)value;

// 无符号short
+ (NSNumber *)numberWithUnsignedShort:(unsigned short)value;

// int
+ (NSNumber *)numberWithInt:(int)value;

// 无符号int
+ (NSNumber *)numberWithUnsignedInt:(unsigned int)value;

// long
+ (NSNumber *)numberWithLong:(long)value;

// 无符号long
+ (NSNumber *)numberWithUnsignedLong:(unsigned long)value;

// long long
+ (NSNumber *)numberWithLongLong:(long long)value;

// 无符号long long
+ (NSNumber *)numberWithUnsignedLongLong:(unsigned long long)value;

// float
+ (NSNumber *)numberWithFloat:(float)value;

// double
+ (NSNumber *)numberWithDouble:(double)value;

// BOOL
+ (NSNumber *)numberWithBool:(BOOL)value;

// NSInteger
+ (NSNumber *)numberWithInteger:(NSInteger)value;

// NSUInteger
+ (NSNumber *)numberWithUnsignedInteger:(NSUInteger)value;

 

二、初始化一个NSNumber对象(装箱)

// char
- (NSNumber *)initWithChar:(char)value;

// 无符号char
- (NSNumber *)initWithUnsignedChar:(unsigned char)value;

// short
- (NSNumber *)initWithShort:(short)value;

// 无符号short
- (NSNumber *)initWithUnsignedShort:(unsigned short)value;

// int
- (NSNumber *)initWithInt:(int)value;

// 无符号int
- (NSNumber *)initWithUnsignedInt:(unsigned int)value;

// long
- (NSNumber *)initWithLong:(long)value;

// 无符号long
- (NSNumber *)initWithUnsignedLong:(unsigned long)value;

// long long
- (NSNumber *)initWithLongLong:(long long)value;

// 无符号long long
- (NSNumber *)initWithUnsignedLongLong:(unsigned long long)value;

// float
- (NSNumber *)initWithFloat:(float)value;

// double
- (NSNumber *)initWithDouble:(double)value;

// BOOL
- (NSNumber *)initWithBool:(BOOL)value;

// NSInteger
- (NSNumber *)initWithInteger:(NSInteger)value;

// NSUInteger
- (NSNumber *)initWithUnsignedInteger:(NSUInteger)value;

 

三、获取NSNumber中的基础数据类型(拆箱)

// char
@property (readonly) char charValue;

// 无符号char
@property (readonly) unsigned char unsignedCharValue;

// short
@property (readonly) short shortValue;

// 无符号short
@property (readonly) unsigned short unsignedShortValue;

// int
@property (readonly) int intValue;

// 无符号int
@property (readonly) unsigned int unsignedIntValue;

// long
@property (readonly) long longValue;

// 无符号long
@property (readonly) unsigned long unsignedLongValue;

// long long
@property (readonly) long long longLongValue;

// 无符号long long
@property (readonly) unsigned long long unsignedLongLongValue;

// float
@property (readonly) float floatValue;

// double
@property (readonly) double doubleValue;

// BOOL
@property (readonly) BOOL boolValue;

// NSInteger
@property (readonly) NSInteger integerValue;

// NSUInteger
@property (readonly) NSUInteger unsignedIntegerValue;

 

四、检索字符串表示

NSNumber *intNun = [NSNumber numberWithChar:"c"];
NSLog(@"~~~~~~~~~~%@", [intNun description]);
NSLog(@"~~~~~~~~~~%@", [intNun descriptionWithLocale:nil]);
====打印
~~~~~~~~~~-125
~~~~~~~~~~-125

 

五、比较NSNumber对象

typedef NS_ENUM(NSInteger, NSComparisonResult) {
    NSOrderedAscending = -1L, //升序(左 < 右)
    NSOrderedSame, // 相等(左 = 右)
    NSOrderedDescending //降序(左 > 右)
};

// 如果两个NSNumber装箱的基本数据类型不一致。按照标准C的基本数据类型转换后,进行比较
- (NSComparisonResult)compare:(NSNumber *)otherNumber;

// 如果是两个数字型的NSNumber对象的比较,用此方法比compare:方法效率更高
- (BOOL)isEqualToNumber:(NSNumber *)number;

标准C语言的基本数据类型介绍,请移步【】

标准C语言的基本数据类型转换规则,请移步【】

 

posted @ 2018-05-22 17:56  码出境界  阅读(522)  评论(0编辑  收藏  举报