int和NSInteger區別


查到c語言中,int和long的字節數是和操作系統指針所占位數相等。

但c語言中說,long的長度永遠大於或等於int

objective-c里,蘋果的官方文檔中總是推薦用NSInteger

它和int有什么區別呢,stackoverflow這幫大神給了答案。

原來在蘋果的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.

總結:NSInteger與int的區別是NSInteger會根據系統的位數(32or64)自動選擇int的最大數值(int or long)。

轉:http://blog.csdn.net/freedom2028/article/details/8035847

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM