int i =100;
float f =3.14;
double db =66.6666;
BOOL bl =YES;
//包装成对象
NSNumber*number1 = [NSNumbernumberWithInt:i];
NSNumber*number2 = [NSNumbernumberWithFloat:f];
NSNumber*number3 = [NSNumbernumberWithDouble:dd];
NSNumber*number4 = [NSNumbernumberWithBool:bl];
NSArray*array =@[number1,number2,number3,number4];
NSLog(@"array = %@",array);
//取出下标为1的元素
NSNumber*n2 = array[1];
NSLog(@"%@",n2);
//拆包装,还原成最初的样子
float ff =[n2floatValue];
NSLog(@"ff = %.2f",ff);
//在Xcode4.6以后的新语法
//10,8.88,3.14,NO
NSArray*array1 =@[@10,@8.88,@3.14,@NO];
NSLog(@"array1 = %@",array1);
NSNumber*nn = array1[1];
doubled = [nndoubleValue];
NSLog(@"d = %.2lf",d);
Person*per = [[Personalloc]init];
per.age=20;
//把per这个对象的年龄保存到数组中
NSArray*array2 =@[@(per.age)];
NSLog(@"array2 = %@",array2);
#import
@interfacePerson :NSObject
@property(nonatomic,assign)intage;
@end
#import"Person.h"
@implementationPerson
@end
网友评论