189 8069 5689

【Objective-C】OC中NSNumber数字对象的常用处理方法

之前已经学习过了基本数据类型。但是这些都市基本数据类型,它们不是对象。因此,不能向它们发送消息。

创新互联是一家专业提供大祥企业网站建设,专注与成都网站设计、网站建设、外贸网站建设H5开发、小程序制作等业务。10年已为大祥众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。

然后有时也需要将它们当做对象来处理。不如,我们需要把基本数据类型放入到集合中(在Cocoa中集中只能存放对象,

无法存放基本数据类型),那么这时我们需要将基本类型转换成数字对象.OC中提供了数据对象”NSNumber“可以将基本数据

类型”包装“成对象,这样我们就可以将基本数据类型来处理了

下面我们来看下具体使用例子:

////  main.m//  FoundationDemo1////  Created by hmjiangqq on 14-1-23.//  Copyright (c) 2014年 hmjiangqq. All rights reserved.//#import int main(int argc,constchar* 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=[monthNumberintValue];float length=[lengthNumberfloatValue];NSLog(@"month=%d\n",month);NSLog(@"length=%.1f\n",length);/*=============NSString====================*/}return0;}
上面只是使用到了NSNumber中的两个包装和还原的方法,在NSNumber中还有很多相类似的方法:

-(id)initWithChar:(char)value;-(id)initWithUnsignedChar:(unsignedchar)value;-(id)initWithShort:(short)value;-(id)initWithUnsignedShort:(unsignedshort)value;-(id)initWithInt:(int)value;-(id)initWithUnsignedInt:(unsignedint)value;-(id)initWithLong:(long)value;-(id)initWithUnsignedLong:(unsignedlong)value;-(id)initWithLongLong:(longlong)value;-(id)initWithUnsignedLongLong:(unsignedlonglong)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;-(unsignedchar)unsignedCharValue;-(short)shortValue;-(unsignedshort)unsignedShortValue;-(int)intValue;-(unsignedint)unsignedIntValue;-(long)longValue;-(unsignedlong)unsignedLongValue;-(longlong)longLongValue;-(unsignedlonglong)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:(unsignedchar)value;+(NSNumber*)numberWithShort:(short)value;+(NSNumber*)numberWithUnsignedShort:(unsignedshort)value;+(NSNumber*)numberWithInt:(int)value;+(NSNumber*)numberWithUnsignedInt:(unsignedint)value;+(NSNumber*)numberWithLong:(long)value;+(NSNumber*)numberWithUnsignedLong:(unsignedlong)value;+(NSNumber*)numberWithLongLong:(longlong)value;+(NSNumber*)numberWithUnsignedLongLong:(unsignedlonglong)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);

网页名称:【Objective-C】OC中NSNumber数字对象的常用处理方法
转载源于:http://cdxtjz.cn/article/jcsodj.html

其他资讯