NSNumber

作者: 史思恒o_0 | 来源:发表于2017-03-06 18:57 被阅读0次

    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

    相关文章

      网友评论

          本文标题:NSNumber

          本文链接:https://www.haomeiwen.com/subject/xcvugttx.html