OC中,集合里面是不能存放基础类型的,我们需要进行包装成对象形式存放,同时对于基础类型和对象类型之间的处理也是通过NSNumber类型来进行处理的。他没有java自带
包装和拆解的功能。
#import "NSNumberStu.h"
@implementation NSNumberStu
-(void) showNumberChange
{
int numberI = 10;
float numberF = 10.f;
long numberL = 10l;
char numberC = 'a';
BOOL numberB = false;
// OC集合中,是不能存放基础类型的,所以必须要包装成对象类型。 这个和java一样,只是jdk1.6后,就自带能够进行自动包装和拆解了。
NSNumber *iCount = [NSNumber numberWithInt:numberI];
NSLog(@"Intenger --- %@",iCount);
NSNumber *fCount = [NSNumber numberWithFloat:numberF];
NSLog(@"Float --- %@",fCount);
NSNumber *lCount = [NSNumber numberWithLong:numberL];
NSLog(@"Long ----%@",lCount);
NSNumber *cCount = [NSNumber numberWithChar:numberC];
NSLog(@"Char ----%@",cCount);
NSNumber *bCount = [NSNumber numberWithBool:numberB];
NSLog(@"Boolean---%@",bCount);
// 上面都是包装成对象类, 下面就是拆分类
NSLog(@"**************************************");
NSLog(@"int ---- %d", [iCount intValue]);
NSLog(@"float ---- %.1f", [fCount floatValue]);
NSLog(@"long ---- %ld", [lCount longValue]);
NSLog(@"char ---- %d", [cCount charValue]);
NSLog(@"bool------%d",[bCount boolValue]);
}
@end
同时NSNumber有很多的静态处理各种类型包装方法,需要之间查看就可以了。
运行结果如下:
2015-01-04 21:45:34.544 OC基础学习[705:47399] Intenger --- 10
2015-01-04 21:45:34.545 OC基础学习[705:47399] Float --- 10
2015-01-04 21:45:34.545 OC基础学习[705:47399] Long ----10
2015-01-04 21:45:34.545 OC基础学习[705:47399] Char ----97
2015-01-04 21:45:34.545 OC基础学习[705:47399] Boolean---0
2015-01-04 21:45:34.545 OC基础学习[705:47399] **************************************
2015-01-04 21:45:34.545 OC基础学习[705:47399] int ---- 10
2015-01-04 21:45:34.545 OC基础学习[705:47399] float ---- 10.0
2015-01-04 21:45:34.546 OC基础学习[705:47399] long ---- 10
2015-01-04 21:45:34.546 OC基础学习[705:47399] char ---- 97
2015-01-04 21:45:34.546 OC基础学习[705:47399] bool------0
Program ended with exit code: 0