85、int 、NSInteger、NSUInteger、NSNumber的区别和联系
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对象有以下方法:
+ numberWithBool:
+ numberWithChar:
+ numberWithDouble:
+ numberWithFloat:
+ numberWithInt:
+ numberWithInteger:
+ numberWithLong:
+ numberWithLongLong:
+ numberWithShort:
+ numberWithUnsignedChar:
+ numberWithUnsignedInt:
+ numberWithUnsignedInteger:
+ numberWithUnsignedLong:
+ numberWithUnsignedLongLong:
+ numberWithUnsignedShort:
初始化方法:
– initWithBool:
– initWithChar:
– initWithDouble:
– initWithFloat:
– initWithInt:
– initWithInteger:
– initWithLong:
– initWithLongLong:
– initWithShort:
– initWithUnsignedChar:
– initWithUnsignedInt:
– initWithUnsignedInteger:
– initWithUnsignedLong:
– initWithUnsignedLongLong:
– initWithUnsignedShort:
检索
– boolValue
– charValue
– decimalValue
– doubleValue
– floatValue
– intValue
– integerValue
– longLongValue
– longValue
– shortValue
– unsignedCharValue
– unsignedIntegerValue
– unsignedIntValue
– unsignedLongLongValue
– unsignedLongValue
– unsignedShortValue
NSNumber类型有点类似id类型,对于任何类型的数字对象都能用它来声明,也就是用它来声明数字对象,通过声明,很难判断声明变量是什么数字类型,确定数字对象类型多是在初始化的时候才能确定。
数字对象的创建或者初始化:
格式:
NSNumber 数字对象 = [NSNumber numberWith数字类型:数值];
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是不行的,比如在一个数组里使用下面的语句就会报错:
NSArray *array = [NSArray alloc] init];
[array addObject:3];
因为array里应该是一个类,但‘3’不是,所以需要用NSNumber:
NSArray *array = [NSArray alloc] init];
[array addObject:[NSNumber numberWithInt:3]]; 苹果官方文档地址
https://developer.apple.com/documentation/foundation/nsnumber
85、int 、NSInteger、NSUInteger、NSNumber的区别和联系的更多相关文章
- int, NSInteger, NSUInteger, NSNumber的区别
新手在接触iOS或者Mac开发的时候,看到int和NSInteger,一般不清楚应该用哪个比较合适.我们先来查看下NSInteger的定义 #if __LP64__ || (TARGET_OS_EMB ...
- object-c中的int NSInteger NSUInteger NSNumber辨析
object-c中的int NSInteger NSUInteger NSNumber辨析 #import <Foundation/Foundation.h> int main(int a ...
- iOS开发之int,NSInteger,NSUInteger,NSNumber的使用
1.首先先了解下NSNumber类型: 苹果官方文档地址:https://developer.apple.com/library/ios/documentation/Cocoa/Reference/F ...
- ios开发 int,NSInteger,NSUInteger,NSNumber
分享一下,在工作工程中遇到的一些不留心的地方: 1.当需要使用int类型的变量的时候,可以像写C的程序一样,用int,也可以用NSInteger,但更推荐使用NSInteger,因为这样就不用考虑设备 ...
- 数字类型(NSInteger,NSUInteger,NSNumber)
在Objective-C中,我们可以使用c中的数字数据类型,int.float.long等.它们都是基本数据类型,而不是对象.也就是说,不能够向它们发送消息.然后,有些时候需要将这些值作为对象使用. ...
- 【学习总结】整理一下int, NSInteger 等概念
基本需要知道的 : unsigned : 没符号的 signed : 有符号的 int : 整型 看看OC的定义 : #if __LP64__ || (TARGET_OS_EMBEDDED & ...
- Hashtable,HashMap,TreeMap有什么区别?Vector,ArrayList,LinkedList有什么区别?int和Integer有什么区别?
接着上篇继续更新. /*请尊重作者劳动成果,转载请标明原文链接:*/ /*https://www.cnblogs.com/jpcflyer/p/10759447.html* / 题目一:Hashtab ...
- C#中Convert.ToInt32、int.TryParse、(int)和int.Parse四者的区别
Convert.ToInt32.(int)和int.Parse三者的区别: 首先:Convert.ToInt32 适合将object类类型转换成int类型,如Convert.ToInt32(sessi ...
- 在java语言中int 和 Integer 有什么区别
在java语言中int 和 Integer 有什么区别 解答:int是基本数据类型,Integer是int的包装类,属于引用类型
随机推荐
- NPOI 修改已存在的excel文件,设置第一行行高
FileStream fileStream = new FileStream(@FileName, FileMode.Open, FileAccess.Read); FileStream fileSt ...
- API & Web API
The follow content refer refers to: Baidu Baike : https://baike.baidu.com/item/api/ ...
- Day04 -玩弄Ruby的方法:instance method与class method
前情提要在第三天时,我们解说了如何在class里用include与extend,去使用module的method. Include is for adding methods to an instan ...
- iOS10以下版本不支持swiper
解决办法,降低swiper4*版本引用低版本如3.4.2可以兼容iOS8,
- echars 图表提示框自定义显示
一 . 显示单条数据时在tooltip里调用formatter函数给自定义提示框内数据. 效果图显示 二 . 当显示多条数据时.为保证和原来的效果相同需要自己实现点的效果.如果不实现,提示框则不限点的 ...
- express 内存溢出问题分析定位
一.现象 1. 如下报错 FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory 1: n ...
- html 自定义上传图片样式,并回显
<div id="photoUpLoad"> <input type="file" id="photo" name=&qu ...
- CodeWarrior 10 自定义关键字模版
==============================================版本信息开始============================================ 相关作 ...
- step_by_step_用python爬点磁力链接
爬点东西 -Scrapy 今天是小年,团聚的日子,想想这一年中发生过大大小小的事,十分感慨. 言归正传: 吐槽了一些话,没事的时候一个单身老男人就只能上上网打发打发时间,后来我发现一个网站比较好,但是 ...
- 【转】图解CSS padding、margin、border属性
http://www.cnblogs.com/linjiqin/p/3556497.html 图解CSS padding.margin.border属性W3C组织建议把所有网页上的对像都放在一个盒(b ...