NSUInteger和NSInteger和int
生活随笔
收集整理的這篇文章主要介紹了
NSUInteger和NSInteger和int
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
int和NSInteger其實(shí)是差不多的,但更推薦使用NSInteger,因?yàn)槭褂肗SInteger,這樣就不用考慮設(shè)備是32位還是64位了。
NSUInteger是無(wú)符號(hào)的,即沒(méi)有負(fù)數(shù),NSInteger是有符號(hào)的,所以NSUInteger類型不能給它賦負(fù)值。比如以下這段代碼,a=-1的時(shí)候,是能進(jìn)入循環(huán)的。
NSUInteger a = -1; for(int i=0;i<a;i++){ NSLog(@"%d,%lu",i,(unsigned long)a); break; } 輸出:0,18446744073709551615 這時(shí)a的值其實(shí)等于18446744073709551615還有NSUInteger和NSInteger和int都是基礎(chǔ)類型,是不能放入NSArray中的,需要轉(zhuǎn)換成NSNumber,應(yīng)為NSNumber是類,NSArray中只能放入類。
用以下NSNumber的類方法轉(zhuǎn)即可
- (NSNumber *) numberWithChar: (char) value;
- (NSNumber *) numberWithInt: (int) value;
- (NSNumber *) numberWithFloat: (float) value;
- (NSNumber *) numberWithBool: (BOOL) value;
原文鏈接:http://www.jianshu.com/p/c883674b6de5
著作權(quán)歸作者所有,轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán),并標(biāo)注“簡(jiǎn)書(shū)作者”。
轉(zhuǎn)載于:https://www.cnblogs.com/Free-Thinker/p/5868289.html
總結(jié)
以上是生活随笔為你收集整理的NSUInteger和NSInteger和int的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 统计字符串中某个字出现的次数
- 下一篇: Linq中string转int的方法