iOS NSInteger/NSUInteger与int/unsigned int、long/unsigned long之间的区别!
在iOS开发中经常使用NSInteger和NSUInteger,而在其他的类似于C++的语言中,我们经常使用的是int、unsigned int。我们知道iOS也可以使用g++编译器,那么它们之间是否有什么联系呢?
从NSUInteger和NSInteger的定义文件中 NSObjCRuntime.h发现有这样的语句:
#if __LP64__ || (TARGET_OS_EMBEDDED && !TARGET_OS_IPHONE) || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
typedef int NSInteger;
typedef unsigned int NSUInteger;
#endif
因此,下面的代码可用改为:
int pvNums = [[[NSUserDefaults standardUserDefaults] objectForKey:@"pvlimit"] integerValue];
修改为:
NSInteger pvNums = [[[NSUserDefaults standardUserDefaults] objectForKey:@"pvlimit"] integerValue];
这里可以清楚的看出NSInteger和int,NSUInteger和unsigned int之间的区别。mac的OS X系统即为__LP64__,而后面则是指具体的目标硬件设备。所以NSInteger/NSUIteger与对应的int/unsigned int不是完全相等的,与对应的long/unsigned long也不是完全相等的。而是要看具体的运行环境及其硬件设备架构。
为了更好的了解上面的定义,可以参考下面说的:
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.
当你不知道程序运行哪种处理器架构时,你最好使用NSInteger,因为在有可能int在32位系统中只是int类型,而在64位系统,int可能变是long型。
I'd stick with using NSInteger instead of int/long unless you specifically require them.
除非不得不使用int/long型,坚持使用NSInteger。
从上面的定义可以看出NSInteger/NSUInteger是一种动态定义的类型,在不同的设备,不同的架构,有可能是int类型,有可能是long类型。
With regard to the correct format specifier you should use for each of these types, see the String Programming Guide's section on Platform Dependencies
为了正确的使用这些类型,可以参考String Programming Guide's section on Platform Dependencies。
为了更简单的知道NSInteger和long的大小,我们只需要记住它们的大小总是等于指针的大小,即在32bit系统中是32bit,在64bit系统大小总是64bit。(NSInteger and long are always pointer-sized. That means they're 32-bits on 32-bit systems, and 64 bits on 64-bit systems.)
总结:
int : 当使用int类型定义变量的时候,可以像写C程序一样,用int也可以用NSInteger,推荐使用NSInteger ,因为这样就不用考虑设备是32位还是64位了。(在32位系统里面NSInterger等价于int,在64位系统里面NSInterger等价于long)
NSUInteger是无符号的,即没有负数,NSInteger是有符号的。
NSInteger是基础类型,NSNumber是一个类,如果需要存储一个数值,直接使用NSInteger是不行的,比如在一个数组里使用下面的语句就会报错:
NSArray *array = [NSArray alloc] init];
[array addObject:];
因为array里应该是一个类,但‘3’不是,所以需要用NSNumber:
NSArray *array = [NSArray alloc] init];
[array addObject:[NSNumber numberWithInt:]];
写的比较简单,希望有帮助。
iOS NSInteger/NSUInteger与int/unsigned int、long/unsigned long之间的区别!的更多相关文章
- iOS开发之int,NSInteger,NSUInteger,NSNumber的使用
1.首先先了解下NSNumber类型: 苹果官方文档地址:https://developer.apple.com/library/ios/documentation/Cocoa/Reference/F ...
- 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 ...
- 深度解析C语言int与unsigned int
就如同int a:一样,int 也能被其它的修饰符修饰.除void类型外,基本数据类型之前都可以加各种类型修饰符,类型修饰符有如下四种:1.signed----有符号,可修饰char.int.Int是 ...
- 坑!坑!坑!防不胜防的unsigned int的运算
我很早之前就知道,unsigned int与int运算的时候,int会被转化为unsigned int来进行运算.一直觉得定这条规则的人是极度反人类的,虽说unsigned int可以表示更大的正值, ...
- char,short ,int ,long,long long,unsigned long long数据范围
from:http://www.cnblogs.com/A123456A/archive/2013/01/25/2876634.html char,short ,int ,long,long long ...
- Ubuntu gcc编译报错:format ‘%llu’ expects argument of type ‘long long unsigned int’, but argument 2 has type ‘__time_t’ [-Wformat=]
平时用的都是Centos系统,今天偶然在Ubuntu下编译了一次代码,发现报错了: 源码: #include <stdio.h> #include <sys/time.h> # ...
- str转unsigned int
用法 1 参数:参数类型为char, 十六进制字符串形式:0X××××××[NUT],十进制字符串形式:×××××××[NUT],字符串的最大长度为16,字符串结尾符必须为ascii码值0(NUT). ...
- 数32位 unsigned int中1的个数
参考文章:http://www.cnblogs.com/graphics/archive/2010/06/21/1752421.html 最简单的方法: int BitCount0(unsigned ...
随机推荐
- [delphi]修改indy源码后重新编译
http://blog.csdn.net/nerdy/article/details/8702568 虽然indy有一身的毛病,但是一般情况下使用起来还是多方便的. 今天在做一个使用到indy的程序的 ...
- What should do in Production
Using Compose in production https://docs.docker.com/compose/production/
- 转载免费的SSL证书
目前我知道的有2种方式进行免费的SSL证书的获取 第一种:腾讯云申请 第二种:Let's Encrypt (国外在) 我一直使用第一种,还可以,有效期1年. 以下转载第二种: 实战申请Let's En ...
- luogu P2085 最小函数值
题目描述 有n个函数,分别为F1,F2,...,Fn.定义Fi(x)=Ai*x^2+Bi*x+Ci (x∈N*).给定这些Ai.Bi和Ci,请求出所有函数的所有函数值中最小的m个(如有重复的要输出多个 ...
- Win10 - 默认图片查看器恢复
1. 新建一个文本文件“1.txt” 2. 在“1.txt”中添加如下代码,并保存: Windows Registry Editor Version 5.00 ; Change Extension's ...
- lib无法访问另外lib中的头文件
工程中app已经有设置User Header Search Paths来包含了需要的头文件,但是个别的lib依然找不到头文件.解决方法: 选择这个lib,在Build Settings中查找选项Use ...
- linux 文件属性、权限、所有人、所属组
Linux命令行模式下,文件还是需要通过ls -l来查看 可以通过ll查看长文件,会有如下类型显示drwxr-xr-x 2 root root 4096 Nov 10 2010 conf 总共有7 ...
- 怎样高速地安装Ubuntu SDK
我在先前的文章"Ubuntu SDK 安装"中已经具体地介绍了怎样安装Ubuntu SDK.可是非常多的开发人员可能在最后安装SDK所须要的chroots时候会失败.这里面的原因是SDK在安装chro ...
- LRUCache 具体解释
LRU的基本概念: LRU是Least Recently Used的缩写,最近最少使用算法. Java 实现LRUCache 1.基于LRU的基本概念,为了达到按最近最少使用排序.能够选择HashMa ...
- cocos2dx3.0 2048多功能版
1.2048项目描写叙述 1.1.2048功能描写叙述 实现手机上2048的功能,同一时候具备能够删除随意一个方块的功能,悔棋功能,退出自己主动保存,启动自己主动载入功能. 1.2.2048所需技术 ...