在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. (47)C#运行时序列化

    序列化是将对象或对象图转化成字节流的过程.反序列化是将字节流转换回对象图的过程.

  2. T2832 6个朋友 codevs

    http://codevs.cn/problem/2832/  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 有这么一种说法: ...

  3. codevs3249搭积木

    3249 搭积木  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description Petya有一个A×B×C的长方体积木,积 ...

  4. Codeforces 961 E Tufurama

    Discription One day Polycarp decided to rewatch his absolute favourite episode of well-known TV seri ...

  5. 用 jQuery实现图片等比例缩放大小

    原文:http://www.open-open.com/code/view/1420975773093 <script type="text/javascript"> ...

  6. Go -- PipleLine

    1.pipeline的产生 从一个现象说起,有一家咖啡吧生意特别好,每天来的客人络绎不绝,客人A来到柜台,客人B紧随其后,客人C排在客人B后面,客人D排在客人C后面,客人E排在客人D后面,一直排到店面 ...

  7. 转:我们是怎么做Code Review的

    我们是怎么做Code Review的   前几天看了<Code Review 程序员的寄望与哀伤>,想到我们团队开展Code Review也有2年了,结果还算比较满意,有些经验应该可以和大 ...

  8. angularjs中下拉框select option默认值

    1.问题说明: option ng-repeat多空白项 2.解决方案: html: <ion-view hide-nav-bar="true"> <ion-co ...

  9. 系统去掉 Android 4.4.2 的StatusBar和NavigationBar

    1. System Bar简单介绍 在Android4.0 (API Level 14)及更高版本号中.System Bar由Status Bar<位于顶部>和Navigation Bar ...

  10. Oracle启动和关闭服务

    Oracle须要启动和关闭的服务: 1.OracleOracle_homeTNSListener     相应于数据库的监听程序 2.OracleServiceSID                 ...