之前已经学习过了基本数据类型。但是这些都市基本数据类型,它们不是对象。因此,不能向它们发送消息。
然后有时也需要将它们当做对象来处理。不如,我们需要把基本数据类型放入到集合中(在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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。