查到c語言中,int和long的字節數是和操做系統指針所佔位數相等。
但c語言中說,long的長度永遠大於或等於int
objective-c裏,蘋果的官方文檔中老是推薦用NSInteger
原來在蘋果的api實現中,NSInteger是一個封裝,它會識別當前操做系統的位數,自動返回最大的類型。
定義的代碼相似於下:objective-c
/#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;
/#endifapi
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.操作系統