NSInteger

查到c语言中,int和long的字节数是和操作系统指针所占位数相等。
但c语言中说,long的长度永远大于或等于int
objective-c里,苹果的官方文档中总是推荐用NSInteger
原来在苹果的api实现中,NSInteger是一个封装,它会识别当前操作系统的位数,自动返回最大的类型。
定义的代码类似于下:

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

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.

    原文作者:酷行僧
    原文地址: https://segmentfault.com/a/1190000000621839
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞