OC-NSNumber

前言

本篇文章介绍OC中的NSNumber

介绍

NSNumber是基本类型的对象表示
我们有时候无法直接使用基本类型,比如我们无法使用基本类型来填充NSArray数组。这个时候可以使用NSNumber

NSNumber的初始化

NSNumber的初始化一般有三种方法,下面介绍:

使用类方法numberWithXXX

+ (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 API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
+ (NSNumber *)numberWithUnsignedInteger:(NSUInteger)value API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));

注意类型NSInteger是一个typedef类型,在64机器上是long类型,在32机器上是int类型。

注意:如果我们使用NSLog打印NSInteger的时候,可以把NSInteger转换为long,然后使用格式化字符"%li"打印。

使用实例方法initWithXXX格式

- (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 API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0)) NS_DESIGNATED_INITIALIZER;
- (NSNumber *)initWithUnsignedInteger:(NSUInteger)value API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0)) NS_DESIGNATED_INITIALIZER;

使用@表达式

看下面的例子

NSNumber* intA = @-1;
NSNumber* longA = @1L;
NSNumber* charA = @'A';
NSNumber* floatA = @10.0f;

注意:我们使用@表达式的时候可以在@后面使用一个表达式,但是表达式一定得用括号括起来,看下面的例子:

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        int a = 1;
        int b = 1;
        char c='o';
        NSNumber* intA = @(a+b);
        NSNumber* charA = @(c);
        NSLog(@"a + b = %@",intA);
        NSLog(@"c = %c",charA.charValue);
    }
    return 0;
}

获取NSNumber的值

我们一般用与初始化匹配的对应类型的方法获取NSNumber的基本类型的值

@property (readonly) char charValue;
@property (readonly) unsigned char unsignedCharValue;
@property (readonly) short shortValue;
@property (readonly) unsigned short unsignedShortValue;
@property (readonly) int intValue;
@property (readonly) unsigned int unsignedIntValue;
@property (readonly) long longValue;
@property (readonly) unsigned long unsignedLongValue;
@property (readonly) long long longLongValue;
@property (readonly) unsigned long long unsignedLongLongValue;
@property (readonly) float floatValue;
@property (readonly) double doubleValue;
@property (readonly) BOOL boolValue;
@property (readonly) NSInteger integerValue API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
@property (readonly) NSUInteger unsignedIntegerValue API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
@property (readonly, copy) NSString *stringValue;

NSNumber的使用

相等判断

- (BOOL)isEqualToNumber:(NSNumber *)number

比较操作

- (NSComparisonResult)compare:(NSNumber *)otherNumber;

对于

[A compare:@1]
  • 如果A<1,结果为NSOrderedAscending
  • 如果A=1,结果为NSOrderedSame
  • 如果A>1,结果为NSOrderedDescending

你可能感兴趣的:(程序设计-Object-C,cocoa,macos,objective-c)