『iOS/Obj-C』The difference among int, NSInteger, NSUInteger and NSNumber

The difference among int, NSInteger, NSUInteger and NSNumber


Apple iOS official document is a pretty superior tool.

About NSNumber:https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/index.html


<1> NSNumber(It's a Class)

NSNumber is a subclass of NSValue that offers a value as any C scalar (numeric) type. 
It defines a set of methods specifically for setting and accessing the value as a signed or unsigned char, short int, int, long int, long long int, float, or double or as a BOOL. (Note that number objects do not necessarily preserve the type they are created with.) 
It also defines a compare: method to determine the ordering of two NSNumberobjects.

1. Inheritance Chain

『iOS/Obj-C』The difference among int, NSInteger, NSUInteger and NSNumber_第1张图片

2. Creating an NSNumber Object

+ numberWithBool:
+ numberWithChar:
+ numberWithDouble:
+ numberWithFloat:
+ numberWithInt:
+ numberWithInteger:
+ numberWithLong:
+ numberWithLongLong:
+ numberWithShort:
+ numberWithUnsignedChar:
+ numberWithUnsignedInt:
+ numberWithUnsignedInteger:
+ numberWithUnsignedLong:
+ numberWithUnsignedLongLong:
+ numberWithUnsignedShort:

3. Initializing an NSNumber Object

- initWithBool:
 Designated Initializer
- initWithChar:
 Designated Initializer
- initWithDouble:
 Designated Initializer
- initWithFloat:
 Designated Initializer
- initWithInt:
 Designated Initializer
- initWithInteger:
 Designated Initializer
- initWithLong:
 Designated Initializer
- initWithLongLong:
 Designated Initializer
- initWithShort:
 Designated Initializer
- initWithUnsignedChar:
 Designated Initializer
- initWithUnsignedInt:
 Designated Initializer
- initWithUnsignedInteger:
 Designated Initializer
- initWithUnsignedLong:
 Designated Initializer
- initWithUnsignedLongLong:
 Designated Initializer
- initWithUnsignedShort:
 Designated Initializer

4. Accessing Numeric Values

boolValue
 Property
charValue
 Property
decimalValue
 Property
doubleValue
 Property
floatValue
 Property
intValue
 Property
integerValue
 Property
longLongValue
 Property
longValue
 Property
shortValue
 Property
unsignedCharValue
 Property
unsignedIntegerValue
 Property
unsignedIntValue
 Property
unsignedLongLongValue
 Property
unsignedLongValue
 Property
unsignedShortValue
 Property

<2> NSInteger (It's NOT a class)

You usually want to use NSInteger when you don't know what kind of processor architecture your code might run on.
So you may for some reason want the largest possible int type, which on 32 bit systems is just an int, while on a 64-bit system it's a long.
That's why NSInteger born.
Attention:some of elder iOS devices are 32-bit systems while new devices are 64-bit system.
This means you are supposed to use "%d" or "%ld" in different situations.

<3> NSUInteger (It's NOT a class)

Actually It is just unsigned NSInteger.

你可能感兴趣的:(iOS/Obj-C,Objective-C,iOS)