『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


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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值