在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之间的区别!的更多相关文章

  1. iOS开发之int,NSInteger,NSUInteger,NSNumber的使用

    1.首先先了解下NSNumber类型: 苹果官方文档地址:https://developer.apple.com/library/ios/documentation/Cocoa/Reference/F ...

  2. int, NSInteger, NSUInteger, NSNumber的区别

    新手在接触iOS或者Mac开发的时候,看到int和NSInteger,一般不清楚应该用哪个比较合适.我们先来查看下NSInteger的定义 #if __LP64__ || (TARGET_OS_EMB ...

  3. object-c中的int NSInteger NSUInteger NSNumber辨析

    object-c中的int NSInteger NSUInteger NSNumber辨析 #import <Foundation/Foundation.h> int main(int a ...

  4. 深度解析C语言int与unsigned int

    就如同int a:一样,int 也能被其它的修饰符修饰.除void类型外,基本数据类型之前都可以加各种类型修饰符,类型修饰符有如下四种:1.signed----有符号,可修饰char.int.Int是 ...

  5. 坑!坑!坑!防不胜防的unsigned int的运算

    我很早之前就知道,unsigned int与int运算的时候,int会被转化为unsigned int来进行运算.一直觉得定这条规则的人是极度反人类的,虽说unsigned int可以表示更大的正值, ...

  6. 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 ...

  7. 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> # ...

  8. str转unsigned int

    用法 1 参数:参数类型为char, 十六进制字符串形式:0X××××××[NUT],十进制字符串形式:×××××××[NUT],字符串的最大长度为16,字符串结尾符必须为ascii码值0(NUT). ...

  9. 数32位 unsigned int中1的个数

    参考文章:http://www.cnblogs.com/graphics/archive/2010/06/21/1752421.html 最简单的方法: int BitCount0(unsigned ...

随机推荐

  1. AC日记——香甜的黄油 codevs 2038

    2038 香甜的黄油 USACO  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description 农夫Jo ...

  2. 王垠:完全用Linux工作 (2003)

    完全用Linux工作,抛弃windows 我已经半年没有使用 Windows 的方式工作了.Linux 高效的完成了我所有的工作. GNU/Linux 不是每个人都想用的.如果你只需要处理一般的事务, ...

  3. Codeforces 757 F Team Rocket Rises Again

    Discription It's the turn of the year, so Bash wants to send presents to his friends. There are n ci ...

  4. delphi函数大全

    delphi函数大全Abort                 函数    引起放弃的意外处理Abs                   函数    绝对值函数AddExitProc          ...

  5. Android使用procrank和dumpsys meminfo 、top分析内存占用情况

    如果你想查看所有进程的内存使用情况,可以使用命令procrank.dumpsys meminfo查看,当然也只可以过滤出某个进程如:dumpsys meminfo | grep -i phone 先来 ...

  6. vs 总结

    1.可以通过 视图--->属性管理器 来直接配置opencv,一键搞定 2.按住shift键不放,然后移动方向键,可以选中一路数据点. 3.调试程序的利器,调用堆栈,可以定位到程序死的那一刻. ...

  7. a#x#i#o#s封装

    封装的js文件如下: /* 用于修改 axios 的一些公用配置,具体参看文档 */import axios from 'axios'import store from '@/store/index. ...

  8. 时间格式 2016-08-15T16:00:00.000Z

    我修改的时间是2016-08-16(转换成Date后默认为2016-08-16 00:00:00),而我得到的时间却是2016-08-15T16:00:00.000Z 联想到我们当前的时区是+8区   ...

  9. JavaScript 模拟键盘事件

    JavaScript 模拟键盘事件和鼠标事件(比如模拟按下回车等) 2016年09月08日 15:23:25 神秘_博士 阅读数:41158 标签: javascript鼠标键盘事件模拟更多 个人分类 ...

  10. 各种加载效果,适合做加载loading动画效果 Eclipse版

    Animation.rar 链接: http://pan.baidu.com/s/1c0QkOz2 密码: kd57