在c語言中,int和long的字節(jié)數(shù)是和操作系統(tǒng)指針所占位數(shù)相等。
但c語言中說,long的長度永遠大于或等于int
objective-c里,蘋果的官方文檔中總是推薦用NSInteger
它和int有什么區(qū)別呢,stackoverflow這幫大神給了答案。
原來在蘋果的api實現(xiàn)中,NSInteger是一個封裝,它會識別當前操作系統(tǒng)的位數(shù),自動返回最大的類型。
定義的代碼類似于下:
#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.
總結:NSInteger與int的區(qū)別是NSInteger會根據(jù)系統(tǒng)的位數(shù)(32or64)自動選擇int的最大數(shù)值(int or long)。
轉:http://blog.csdn.net/freedom2028/article/details/8035847