iOS开发之int,NSInteger,NSUInteger,NSNumber的使用
2015-12-22 09:22
573 查看
1、当需要使用int类型的变量的时
4000
候,可以像写C的程序一样,用int,也可以用NSInteger,但更推荐使用NSInteger,因为这样就不用考虑设备是32位的还是64位的。
2、NSUInteger是无符号的,即没有负数,NSInteger是有符号的。
3、有人说既然都有了NSInteger等这些基础类型了为什么还要有NSNumber?它们的功能当然是不同的。
NSInteger是基础类型,但是NSNumber是一个类。如果想要存储一个数值,直接用NSInteger是不行的,比如在一个Array里面这样用:
这样是会引发编译错误的,因为NSArray里面放的需要是一个类,但‘3’不是。这个时候需要用到NSNumber:
Cocoa提供了NSNumber类来包装(即以对象形式实现)基本数据类型。
例如以下创建方法:
将基本类型数据封装到NSNumber中后,就可以通过下面的实例方法重新获取它:
4000
候,可以像写C的程序一样,用int,也可以用NSInteger,但更推荐使用NSInteger,因为这样就不用考虑设备是32位的还是64位的。
2、NSUInteger是无符号的,即没有负数,NSInteger是有符号的。
3、有人说既然都有了NSInteger等这些基础类型了为什么还要有NSNumber?它们的功能当然是不同的。
NSInteger是基础类型,但是NSNumber是一个类。如果想要存储一个数值,直接用NSInteger是不行的,比如在一个Array里面这样用:
NSArray *array= [[NSArray alloc]init]; [array addObject:3];//会编译错误
这样是会引发编译错误的,因为NSArray里面放的需要是一个类,但‘3’不是。这个时候需要用到NSNumber:
NSArray *array= [[NSArray alloc]init]; [array addObject:[NSNumber numberWithInt:3]];
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;
相关文章推荐
- requirejs的简要用法
- [to do list][PCB][questions]and[plan]
- LeetCode "Shortest Distance from All Buildings"
- Leetcode: Count Univalue Subtrees
- [leetcode] 304. Range Sum Query 2D - Immutable 解题报告
- [leetcode] 303. Range Sum Query - Immutable 解题报告
- Divide and Conquer:Cable Master(POJ 1064)
- Team Queue UVA540
- java中ProcessBuilder
- js实现C#的StringBuilder效果完整实例
- Divide and Conquer:Monthly Expense(POJ 3273)
- Request的getParameter和getAttribute方法的区别
- request.getScheme() 使用方法
- JavaSE的UI界面编程
- CALayer 花瓣飘落 转场动画-CATransation 动画组 UIView动画封装
- iOS - UIImageView
- iOS- UIProgressView
- UI相关基础知识
- BlockQueue使用之简单应用
- UIScrollView,UITableView的简单使用