黑马程序员--NSNumber NSValue

 -----------android培训、java培训、java学习型技术博客、期待与您交流!------------

  -----------并不是生来就是大神、代码量决定一切、其他都是假象、----------- 

NSNumber 用来包装几本数据库类型中的数字
问:为什么要学NSNumber
答:因为在数组 活着 字典中 ,不能直接存放数字 但是有些时候我们需要存放数着 这个方法可以讲数字包装成对象然后导入其中
         //1.在数组或者字典中快捷输入方式:@10\@1.5\@YES\int i = 0 @(i)

        //这些包装的数字,返回值就是NSNumber

        int a = 10;

        NSMutableDictionary *dict = [NSMutableDictionary   dictionaryWithObjectsAndKeys:@"jack",@"name",@(a),@"age"nil];

        //@(a) = 10; 也就是说 直接打印NSNumber对象 可以得到它的值

        

        //2.NSNumber对象中的基本数据类型

//        -(char)charValue; -(int)intcharValue,-(long)longcharValue,-(double)doublecharValue,-(BOOL)boolcharValue

//-(NSString *)NSStringcharValue,-(NSComparisonResult)compare:(NSNumber *)otherNumber:other,-[(BOOL)isEqualToNumber:(NSNumber*)number

        //

        //创建一个int类型的NSNumber对象

        NSNumber *n1 = @10;

        //n1对象的值传给int b

        int b = [n1 intValue];

        NSLog(@"%d",b);

        //记录点

        int i = 10;

        NSNumber *num = [NSNumber numberWithInt:10];//创建Number对象

        NSNumber *n = @12;//快速创建

        NSMutableDictionary *d = [NSMutableDictionary dictionaryWithObject:num forKey:@"age"];//导入数组

        NSDictionary  *d1 = [NSDictionary dictionaryWithObject:@20 forKey:@"age"];//快速包装

        NSLog(@"%@",d[@"age"]);

        //or

        NSDictionary *ary = @{@"age" : @20};

        NSLog(@"%@",ary[@"age"]);

        //3类型转换

        NSNumber *m = @12;

        int a1 = [m intValue];

        int a2 = [@12 intValue];

        NSLog(@"%d",a2);

                NSValue  用来包装 Size Point Rect surest 等 因为想着中结构体也是不能导入数组或者字典中的 需要包装一下才能导入

        //练习点

        NSSize size = CGSizeMake(10050);//包装size类型

        NSPoint p = CGPointMake(00);

        NSRect rect = CGRectMake(0010050);

        NSValue *val = [NSValue valueWithBytes:&size objCType:@encode(NSSize)];

        NSValue *val1 = [NSValue valueWithBytes:&p objCType:@encode(NSPoint) ];

        NSDictionary  *dict = [NSDictionary dictionaryWithObject:val forKey:@"xy"];

        NSLog(@"%@",dict[@"xy"]);

        NSDictionary *dict1 = [NSDictionary dictionaryWithObjectsAndKeys:val1,@"point",val,@"size"nil];

        NSLog(@"%@",dict1[@"size"]);

        NSValue *val2 = [NSValue valueWithRect:rect];

        NSDictionary *dict2 =   @{@"point" : val1 , @"size" : val , @"rect" : val2};
        
NSLog(@"%@",dict2[@"size"]);


               //NSValue 练习点

        typedef struct {

            int year;

            int month;

            int day;

            

        }Date;

        

        Date lulu = {1983,1,29};

        NSValue *birthday = [NSValue valueWithBytes:&lulu3 objCType:@encode(Date) ];


        NSDictionary *dict = @{@"name" : @"lulu" , @"qq" : @"982580186" , @"age" : @32 , @"home" : @"china" , @"sex" : @"Man",@"birthday" : lulu3};

        

        

        Date lulu2;

        [birthday getValue:&lulu2];

        NSLog(@"%d,%d,%d",lulu2.year,lulu2.month,lulu2.day);




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值