【iOS知识学习】_int、NSInteger、NSUInteger、NSNumber的区别和联系
2015-11-16 22:48
483 查看
1、首先先了解下NSNumber类型:
苹果官方文档地址:https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/Reference/Reference.html
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
+ numberWithBool:
+ numberWithChar:
+ numberWithDouble:
+ numberWithFloat:
+ numberWithInt:
+ numberWithInteger:
+ numberWithLong:
+ numberWithLongLong:
+ numberWithShort:
+ numberWithUnsignedChar:
+ numberWithUnsignedInt:
+ numberWithUnsignedInteger:
+ numberWithUnsignedLong:
+ numberWithUnsignedLongLong:
+ numberWithUnsignedShort:
初始化方法:
[objc] view
plaincopy
– initWithBool:
– initWithChar:
– initWithDouble:
– initWithFloat:
– initWithInt:
– initWithInteger:
– initWithLong:
– initWithLongLong:
– initWithShort:
– initWithUnsignedChar:
– initWithUnsignedInt:
– initWithUnsignedInteger:
– initWithUnsignedLong:
– initWithUnsignedLongLong:
– initWithUnsignedShort:
检索
[objc] view
plaincopy
– boolValue
– charValue
– decimalValue
– doubleValue
– floatValue
– intValue
– integerValue
– longLongValue
– longValue
– shortValue
– unsignedCharValue
– unsignedIntegerValue
– unsignedIntValue
– unsignedLongLongValue
– unsignedLongValue
– unsignedShortValue
NSNumber类型有点类似id类型,对于任何类型的数字对象都能用它来声明,也就是用它来声明数字对象,通过声明,很难判断声明变量是什么数字类型,确定数字对象类型多是在初始化的时候才能确定。
数字对象的创建或者初始化:
格式:
NSNumber 数字对象 = [NSNumber numberWith数字类型:数值];
[objc] view
plaincopy
intNumber = [NSNumber numberWithInt:100];
longNumber = [NSNumber numberWithLong:0xabcdef];
floatNumber = [NSNumber numberWithFloat:10.01];
2、int、 NSInteger、 NSUInteger、NSNumber之间的区别和联系
int : 当使用int类型定义变量的时候,可以像写C程序一样,用int也可以用NSInteger,推荐使用NSInteger ,因为这样就不用考虑设备是32位还是64位了。
NSUInteger是无符号的,即没有负数,NSInteger是有符号的。
NSInteger是基础类型,NSNumber是一个类,如果需要存储一个数值,直接使用NSInteger是不行的,比如在一个数组里使用下面的语句就会报错:
[objc] view
plaincopy
NSArray *array = [NSArray alloc] init];
[array addObject:3];
因为array里应该是一个类,但‘3’不是,所以需要用NSNumber:
[objc] view
plaincopy
NSArray *array = [NSArray alloc] init];
[array addObject:[NSNumber numberWithInt:3]];
苹果官方文档地址:https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/Reference/Reference.html
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
+ numberWithBool:
+ numberWithChar:
+ numberWithDouble:
+ numberWithFloat:
+ numberWithInt:
+ numberWithInteger:
+ numberWithLong:
+ numberWithLongLong:
+ numberWithShort:
+ numberWithUnsignedChar:
+ numberWithUnsignedInt:
+ numberWithUnsignedInteger:
+ numberWithUnsignedLong:
+ numberWithUnsignedLongLong:
+ numberWithUnsignedShort:
初始化方法:
[objc] view
plaincopy
– initWithBool:
– initWithChar:
– initWithDouble:
– initWithFloat:
– initWithInt:
– initWithInteger:
– initWithLong:
– initWithLongLong:
– initWithShort:
– initWithUnsignedChar:
– initWithUnsignedInt:
– initWithUnsignedInteger:
– initWithUnsignedLong:
– initWithUnsignedLongLong:
– initWithUnsignedShort:
检索
[objc] view
plaincopy
– boolValue
– charValue
– decimalValue
– doubleValue
– floatValue
– intValue
– integerValue
– longLongValue
– longValue
– shortValue
– unsignedCharValue
– unsignedIntegerValue
– unsignedIntValue
– unsignedLongLongValue
– unsignedLongValue
– unsignedShortValue
NSNumber类型有点类似id类型,对于任何类型的数字对象都能用它来声明,也就是用它来声明数字对象,通过声明,很难判断声明变量是什么数字类型,确定数字对象类型多是在初始化的时候才能确定。
数字对象的创建或者初始化:
格式:
NSNumber 数字对象 = [NSNumber numberWith数字类型:数值];
[objc] view
plaincopy
intNumber = [NSNumber numberWithInt:100];
longNumber = [NSNumber numberWithLong:0xabcdef];
floatNumber = [NSNumber numberWithFloat:10.01];
2、int、 NSInteger、 NSUInteger、NSNumber之间的区别和联系
int : 当使用int类型定义变量的时候,可以像写C程序一样,用int也可以用NSInteger,推荐使用NSInteger ,因为这样就不用考虑设备是32位还是64位了。
NSUInteger是无符号的,即没有负数,NSInteger是有符号的。
NSInteger是基础类型,NSNumber是一个类,如果需要存储一个数值,直接使用NSInteger是不行的,比如在一个数组里使用下面的语句就会报错:
[objc] view
plaincopy
NSArray *array = [NSArray alloc] init];
[array addObject:3];
因为array里应该是一个类,但‘3’不是,所以需要用NSNumber:
[objc] view
plaincopy
NSArray *array = [NSArray alloc] init];
[array addObject:[NSNumber numberWithInt:3]];
相关文章推荐
- iOS开发之路--微博“更多”页面
- Objective-C的内省(Introspection)用法小结
- Objective-C中常用的结构体NSRange,NSPoint,NSSize(CGSize),NSRect实例分析
- iOS开发之路--微博骨架搭建
- Objective-C中NSNumber与NSDictionary的用法简介
- IOS开发代码分享之获取启动画面图片的string
- Objective-C中NSLog输出格式大全
- 全面解析Objective-C中的block代码块的使用
- Swift调用Objective-C编写的API实例
- Swift、Objective-C、Cocoa混合编程设置指南
- Objective-c代码如何移植为Swift代码 Objective-c代码转移到Swift过程介绍
- Swift调用Objective-C代码
- IOS开发之路--C语言数组和字符串
- IOS开发代码分享之用nstimer实现倒计时功能
- 以实例讲解Objective-C中的KVO与KVC机制
- objective-c中生成随机数的方法
- 在Swift中使用Objective-C编写类、继承Objective-C类
- IOS开发代码分享之设置UISearchBar的背景颜色
- Swift能代替Objective-C吗?
- Objective-C 消息传递机制详解