在以后的学习或者以后做项目的时候 会希望把基本数据类型,例如:int、float等等数值 也存到数组或者字典中
因为数组、字典只能存储对象
所以,NSNumber类可以对基本数据进行封装成一个对象 进行存储
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { NSNumber * intNumber = [NSNumber numberWithInt:11]; NSNumber * floatNumber = [NSNumber numberWithFloat:2.1f]; NSNumber * doubleNumber = [NSNumber numberWithFloat:2.2]; NSNumber * charNumber = [NSNumber numberWithChar:'r']; NSNumber * boolNumber = [NSNumber numberWithBool:YES]; //得到intNumber中得值 int a = [intNumber intValue]; NSInteger aa = [intNumber integerValue]; //把他们放在数组中 NSArray * array = @[intNumber,floatNumber,doubleNumber,charNumber,boolNumber]; //把他们放在字典中 NSDictionary * dict = @{@"int":intNumber,@"float":floatNumber,@"double":doubleNumber,@"char":charNumber,@"bool":boolNumber}; } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-16 22:49:47