int和NSInteger區別

來源:互聯網
上載者:User

標籤:blog   http   os   ar   for   2014   art   cti   代碼   

查到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

 

int和NSInteger區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.