您好,登錄后才能下訂單哦!
之前已經學習過了基本數據類型。但是這些都市基本數據類型,它們不是對象。因此,不能向它們發送消息。
然后有時也需要將它們當做對象來處理。不如,我們需要把基本數據類型放入到集合中(在Cocoa中集中只能存放對象,
無法存放基本數據類型),那么這時我們需要將基本類型轉換成數字對象.OC中提供了數據對象”NSNumber“可以將基本數據
類型”包裝“成對象,這樣我們就可以將基本數據類型來處理了
下面我們來看下具體使用例子:
// // main.m // FoundationDemo1 // // Created by hmjiangqq on 14-1-23. // Copyright (c) 2014年 hmjiangqq. All rights reserved. // #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... NSLog(@"Hello, World!"); /*===========NSNumber=================*/ //創建一個NSNumber對象 NSNumber *monthNumber=[[NSNumber alloc]initWithInt:10]; NSNumber *lengthNumber=[[NSNumber alloc]initWithFloat:10.8]; NSLog(@"month =%@\n",monthNumber); NSLog(@"length =%@\n",lengthNumber); //還原成基本數據類型 int month=[monthNumber intValue]; float length=[lengthNumber floatValue]; NSLog(@"month=%d\n",month); NSLog(@"length=%.1f\n",length); /*=============NSString====================*/ } return 0; }上面只是使用到了NSNumber中的兩個包裝和還原的方法,在NSNumber中還有很多相類似的方法:
- (id)initWithChar:(char)value; - (id)initWithUnsignedChar:(unsigned char)value; - (id)initWithShort:(short)value; - (id)initWithUnsignedShort:(unsigned short)value; - (id)initWithInt:(int)value; - (id)initWithUnsignedInt:(unsigned int)value; - (id)initWithLong:(long)value; - (id)initWithUnsignedLong:(unsigned long)value; - (id)initWithLongLong:(long long)value; - (id)initWithUnsignedLongLong:(unsigned long long)value; - (id)initWithFloat:(float)value; - (id)initWithDouble:(double)value; - (id)initWithBool:(BOOL)value; - (id)initWithInteger:(NSInteger)value NS_AVAILABLE(10_5, 2_0); - (id)initWithUnsignedInteger:(NSUInteger)value NS_AVAILABLE(10_5, 2_0);與之相反的方法如下:
- (char)charValue; - (unsigned char)unsignedCharValue; - (short)shortValue; - (unsigned short)unsignedShortValue; - (int)intValue; - (unsigned int)unsignedIntValue; - (long)longValue; - (unsigned long)unsignedLongValue; - (long long)longLongValue; - (unsigned long long)unsignedLongLongValue; - (float)floatValue; - (double)doubleValue; - (BOOL)boolValue; - (NSInteger)integerValue NS_AVAILABLE(10_5, 2_0); - (NSUInteger)unsignedIntegerValue NS_AVAILABLE(10_5, 2_0);當然我們還可以使用NSNumber的靜態方法進行創建對象,方法如下:
+ (NSNumber *)numberWithChar:(char)value; + (NSNumber *)numberWithUnsignedChar:(unsigned char)value; + (NSNumber *)numberWithShort:(short)value; + (NSNumber *)numberWithUnsignedShort:(unsigned short)value; + (NSNumber *)numberWithInt:(int)value; + (NSNumber *)numberWithUnsignedInt:(unsigned int)value; + (NSNumber *)numberWithLong:(long)value; + (NSNumber *)numberWithUnsignedLong:(unsigned long)value; + (NSNumber *)numberWithLongLong:(long long)value; + (NSNumber *)numberWithUnsignedLongLong:(unsigned long long)value; + (NSNumber *)numberWithFloat:(float)value; + (NSNumber *)numberWithDouble:(double)value; + (NSNumber *)numberWithBool:(BOOL)value; + (NSNumber *)numberWithInteger:(NSInteger)value NS_AVAILABLE(10_5, 2_0); + (NSNumber *)numberWithUnsignedInteger:(NSUInteger)value NS_AVAILABLE(10_5, 2_0);
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。