在編程的過程中,一些基本數(shù)據(jù)類型的變量是必不可少的蝌数。但是在OC編程語言當(dāng)中,OC的消息機(jī)制并不能向這些基本數(shù)據(jù)類型發(fā)送消息顶伞,而且OC的集合當(dāng)中是不能存放基本數(shù)據(jù)類型的饵撑,所以我們就需要將基本數(shù)據(jù)類型轉(zhuǎn)化成對象來使用枝哄。OC中提供了NSNumber類來將基本數(shù)據(jù)類型“包裝”成數(shù)字對象阻荒,這樣我們就可以將基本數(shù)據(jù)類型當(dāng)做對象來處理了挠锥。
1.NSNumber基本使用
NSNumber可以將基本數(shù)據(jù)類型包裝成對象,也可以將包裝后的對象還原成基本數(shù)據(jù)類型蓖租,以下演示常用的幾種基本數(shù)據(jù)類型的包裝過程:
// 封裝基本數(shù)據(jù)類型(int,float,double,char,BOOL)
intbasicInt=6;
floatbasicFloat=3.14;
doublebasicDouble=0.85;
charbasicChar='c';
BOOL basicBool=1;
// 創(chuàng)建int型對象
NSNumber*objectInt=[NSNumbernumberWithInt:basicInt];
NSLog(@"objectInt = %@",objectInt);
NSLog(@"還原后:%d",[objectInt intValue]);
// 創(chuàng)建float型對象
NSNumber*objectFloat=[NSNumbernumberWithFloat:basicFloat];
NSLog(@"objectFloat = %@",objectFloat);
NSLog(@"還原后:%f",[objectFloat floatValue]);
// 創(chuàng)建double型對象
NSNumber*objectDouble=[NSNumbernumberWithDouble:basicDouble];
NSLog(@"objectDouble = %@",objectDouble);
NSLog(@"還原后:%f",[objectDouble doubleValue]);
// 創(chuàng)建char型對象
NSNumber*objectChar=[NSNumbernumberWithChar:basicChar];
NSLog(@"objectChar = %@",objectChar);// 打印出ASCII碼值
NSLog(@"還原后:%c",[objectChar charValue]);
// 創(chuàng)建BOOL型對象
NSNumber*objectBool=[NSNumbernumberWithBool:basicBool];
NSLog(@"objectBOOL = %@",objectBool);
NSLog(@"還原后:%d",[objectBool boolValue]);
//快速創(chuàng)建的方法
NSNumber*intNum=@80;// --->? [NSNumber numberWithInt:80];
NSNumber*floatNum=@3.14;// --->[NSNumber numberWithFloat:3.14];
NSNumber*boolNum=@YES;// ---> [NSNumber numberWithBool:YES];
NSNumber*charNum=@'a';// ---> [NSNumber numberWithChar:'a'];
// 快速創(chuàng)建變量需要將變量放在括號中
doubleheight=1.83;
NSNumber*doubleNum=@(height);// ---> [NSNumber numberWithDouble:height];
NSArray*array=@[intNum,floatNum,boolNum,charNum,doubleNum];
NSLog(@"array : %@",array);
//判斷兩個對象的值是否相等
if([intNum isEqualToNumber:floatNum]==YES){
NSLog(@"值相等");
}else{
NSLog(@"值不相等");
}
//比較兩個對象的值大小
NSNumber*myNumber=@30;
if([intNum compare:myNumber]==NSOrderedAscending){
NSLog(@"第一個數(shù)字小");
}else{
NSLog(@"第一個數(shù)字大");
}
類方法創(chuàng)建一般都會有相應(yīng)的實(shí)例初始化方法與之對應(yīng):
-(nullable instancetype)initWithCoder:(NSCoder*)aDecoder NS_DESIGNATED_INITIALIZER;
-(NSNumber*)initWithChar:(char)value NS_DESIGNATED_INITIALIZER;
-(NSNumber*)initWithUnsignedChar:(unsignedchar)value NS_DESIGNATED_INITIALIZER;
-(NSNumber*)initWithShort:(short)value NS_DESIGNATED_INITIALIZER;
-(NSNumber*)initWithUnsignedShort:(unsignedshort)value NS_DESIGNATED_INITIALIZER;
-(NSNumber*)initWithInt:(int)value NS_DESIGNATED_INITIALIZER;
-(NSNumber*)initWithUnsignedInt:(unsignedint)value NS_DESIGNATED_INITIALIZER;
-(NSNumber*)initWithLong:(long)value NS_DESIGNATED_INITIALIZER;
-(NSNumber*)initWithUnsignedLong:(unsignedlong)value NS_DESIGNATED_INITIALIZER;
-(NSNumber*)initWithLongLong:(longlong)value NS_DESIGNATED_INITIALIZER;
-(NSNumber*)initWithUnsignedLongLong:(unsignedlonglong)value NS_DESIGNATED_INITIALIZER;
-(NSNumber*)initWithFloat:(float)value NS_DESIGNATED_INITIALIZER;
-(NSNumber*)initWithDouble:(double)value NS_DESIGNATED_INITIALIZER;
-(NSNumber*)initWithBool:(BOOL)value NS_DESIGNATED_INITIALIZER;
-(NSNumber*)initWithInteger:(NSInteger)value NS_AVAILABLE(10_5,2_0)NS_DESIGNATED_INITIALIZER;
-(NSNumber*)initWithUnsignedInteger:(NSUInteger)value NS_AVAILABLE(10_5,2_0)NS_DESIGNATED_INITIALIZER;
2.int、 NSInteger蓖宦、 NSUInteger、NSNumber之間的區(qū)別和聯(lián)系
int : 當(dāng)使用int類型定義變量的時候稠茂,可以像寫C程序一樣去使用柠偶。當(dāng)你不知道程序運(yùn)行在哪種處理器架構(gòu)時睬关,你最好使用NSInteger诱担,因?yàn)樵谟锌赡躨nt在32位系統(tǒng)中只是int類型电爹,而在64位系統(tǒng),int可能變成long型丐箩。除非不得不使用int/long型摇邦,否則推薦使用NSInteger屎勘。
NSInteger/NSUInteger是一種動態(tài)定義的類型施籍,在不同的設(shè)備挑秉,不同的架構(gòu),有可能是int類型犀概,有可能是long類型立哑。NSUInteger是無符號的姻灶,即沒有負(fù)數(shù)铛绰,NSInteger是有符號的产喉。
NSInteger是基礎(chǔ)類型,NSNumber是一個類曾沈,如果需要存儲一個數(shù)值这嚣,直接使用NSInteger是不行的,因?yàn)镺C的集合當(dāng)中存儲的數(shù)據(jù)必須是OC對象姐帚。