int,NSInteger,NSUInteger,NSNumber区别
2014-11-22 03:38
519 查看
1.当需要使用int类型的变量的时候,可以像写C的程序一样,用int,也可以用NSInteger,但更推荐使用NSInteger,因为这样就不用考虑设备是32位的还是64位的。
2.NSUInteger是无符号的,即没有负数,NSInteger是有符号的。
3.NSNumber类型有点类似id类型,对于任何类型的数字对象都能用它来声明,也就是用它来声明数字对象,通过声明,很难判断声明变量是什么数字类型,确定数字对象类型多是在初始化的时候才能确定。
数字对象的创建或者初始化:
格式:
NSNumber 数字对象 = [NSNumber numberWith数字类型:数值];
NSNumber是NSValue的一个子类,它是一个对象来存储数字值包括bool型,它提供了一系列的方法来存储char a signed or unsigned char, short int, int, long int, long long int, float, or double or as a BOOL,它提供了一个compare:方法来决定两个NSNumber对象的排序;
创建一个NSNumber对象有以下方法:
[objc] view
plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
+ numberWithBool:
+ numberWithChar:
+ numberWithDouble:
+ numberWithFloat:
+ numberWithInt:
+ numberWithInteger:
+ numberWithLong:
+ numberWithLongLong:
+ numberWithShort:
+ numberWithUnsignedChar:
+ numberWithUnsignedInt:
+ numberWithUnsignedInteger:
+ numberWithUnsignedLong:
+ numberWithUnsignedLongLong:
+ numberWithUnsignedShort:
初始化方法:
[objc] view
plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
– initWithBool:
– initWithChar:
– initWithDouble:
– initWithFloat:
– initWithInt:
– initWithInteger:
– initWithLong:
– initWithLongLong:
– initWithShort:
– initWithUnsignedChar:
– initWithUnsignedInt:
– initWithUnsignedInteger:
– initWithUnsignedLong:
– initWithUnsignedLongLong:
– initWithUnsignedShort:
检索
[objc] view
plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
– boolValue
– charValue
– decimalValue
– doubleValue
– floatValue
– intValue
– integerValue
– longLongValue
– longValue
– shortValue
– unsignedCharValue
– unsignedIntegerValue
– unsignedIntValue
– unsignedLongLongValue
– unsignedLongValue
– unsignedShortValue
NSInteger是基础类型,但是NSNumber是一个类。如果想要存储一个数值,直接用NSInteger是不行的,比如在一个Array里面这样用:
这样是会引发编译错误的,因为NSArray里面放的需要是一个类,但‘3’不是。这个时候需要用到NSNumber:
Cocoa提供了NSNumber类来包装(即以对象形式实现)基本数据类型。
例如以下创建方法:
+ (NSNumber *)
numberWithChar: (char) value;
+ (NSNumber *)
numberWithInt: (int) value;
+ (NSNumber *)
numberWithFloat: (float) value;
+ (NSNumber *)
numberWithBool: (BOOL) value;
将基本类型数据封装到NSNumber中后,就可以通过下面的实例方法重新获取它:
- (char)
charValue;
- (int)
intValue;
- (float)
floatValue;
- (BOOL) boolValue;
- (NSString *)
stringValue;
2.NSUInteger是无符号的,即没有负数,NSInteger是有符号的。
3.NSNumber类型有点类似id类型,对于任何类型的数字对象都能用它来声明,也就是用它来声明数字对象,通过声明,很难判断声明变量是什么数字类型,确定数字对象类型多是在初始化的时候才能确定。
数字对象的创建或者初始化:
格式:
NSNumber 数字对象 = [NSNumber numberWith数字类型:数值];
NSNumber是NSValue的一个子类,它是一个对象来存储数字值包括bool型,它提供了一系列的方法来存储char a signed or unsigned char, short int, int, long int, long long int, float, or double or as a BOOL,它提供了一个compare:方法来决定两个NSNumber对象的排序;
创建一个NSNumber对象有以下方法:
[objc] view
plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
+ numberWithBool:
+ numberWithChar:
+ numberWithDouble:
+ numberWithFloat:
+ numberWithInt:
+ numberWithInteger:
+ numberWithLong:
+ numberWithLongLong:
+ numberWithShort:
+ numberWithUnsignedChar:
+ numberWithUnsignedInt:
+ numberWithUnsignedInteger:
+ numberWithUnsignedLong:
+ numberWithUnsignedLongLong:
+ numberWithUnsignedShort:
初始化方法:
[objc] view
plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
– initWithBool:
– initWithChar:
– initWithDouble:
– initWithFloat:
– initWithInt:
– initWithInteger:
– initWithLong:
– initWithLongLong:
– initWithShort:
– initWithUnsignedChar:
– initWithUnsignedInt:
– initWithUnsignedInteger:
– initWithUnsignedLong:
– initWithUnsignedLongLong:
– initWithUnsignedShort:
检索
[objc] view
plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
– boolValue
– charValue
– decimalValue
– doubleValue
– floatValue
– intValue
– integerValue
– longLongValue
– longValue
– shortValue
– unsignedCharValue
– unsignedIntegerValue
– unsignedIntValue
– unsignedLongLongValue
– unsignedLongValue
– unsignedShortValue
NSInteger是基础类型,但是NSNumber是一个类。如果想要存储一个数值,直接用NSInteger是不行的,比如在一个Array里面这样用:
NSArray *array = [[NSArray alloc]init]; [array addObject:3];//会编译错误 |
NSArray *array = [[NSArray alloc]init]; [array addObject:[NSNumber numberWithInt:3]]; |
例如以下创建方法:
+ (NSNumber *)
numberWithChar: (char) value;
+ (NSNumber *)
numberWithInt: (int) value;
+ (NSNumber *)
numberWithFloat: (float) value;
+ (NSNumber *)
numberWithBool: (BOOL) value;
将基本类型数据封装到NSNumber中后,就可以通过下面的实例方法重新获取它:
- (char)
charValue;
- (int)
intValue;
- (float)
floatValue;
- (BOOL) boolValue;
- (NSString *)
stringValue;
相关文章推荐
- 【iOS】_int、NSInteger、NSUInteger、NSNumber的区别和联系
- 【iOS知识学习】_int、NSInteger、NSUInteger、NSNumber的区别和联系
- int、NSInteger、NSUInteger、NSNumber的区别和联系
- 【iOS知识学习】_int、NSInteger、NSUInteger、NSNumber的区别和联系
- _int、NSInteger、NSUInteger、NSNumber的区别和联系
- 【iOS知识学习】_int、NSInteger、NSUInteger、NSNumber的区别和联系
- int、NSInteger、NSUInteger、NSNumber的区别和联系
- int, NSInteger, NSUInteger, NSNumber的区别
- int、NSInteger、NSUInteger、NSNumber的区别和联系
- 【iOS知识学习】_int、NSInteger、NSUInteger、NSNumber的区别和联系
- int、 NSInteger、 NSUInteger、NSNumber之间的区别
- _int、NSInteger、NSUInteger、NSNumber的区别和联系
- _int、NSInteger、NSUInteger、NSNumber的区别和联系
- int、NSInteger、NSUInteger、NSNumber的区别和联系
- 【iOS知识学习】_int、NSInteger、NSUInteger、NSNumber的区别和联系
- int, NSInteger, NSUInteger, NSNumber的区别
- 基础知识之_int、NSInteger、NSUInteger、NSNumber的区别和联系
- 【iOS知识学习】_int、NSInteger、NSUInteger、NSNumber的区别和联系
- int、NSInteger、NSUInteger、NSNumber的关系
- 『iOS/Obj-C』The difference among int, NSInteger, NSUInteger and NSNumber