• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Objective-c-NSNumber和NSValue

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

NSNumber

        //NSNumber创建后可以放入数组或字典中     装箱(boxing)基本类型->对象     取消装箱(unboxing)对象->基本类型
NSNumber *n1 = [NSNumber numberWithInt:100];
NSLog(@"n1: %@", n1);

NSMutableArray *arr1 = [NSMutableArray arrayWithCapacity:10];
[arr1 addObject:n1];
NSLog(@"arr1: %@", arr1);

NSMutableDictionary *dic1 = [NSMutableDictionary dictionaryWithCapacity:10];
[dic1 setObject:n1 forKey:@"price"];
NSLog(@"dic1: %@", dic1);


//NSNumber初始化
NSNumber *n2 = [NSNumber numberWithChar:'a'];
NSLog(@"n2: %@", n2);

NSNumber *n3 = [NSNumber numberWithBool:false];
NSLog(@"n3: %@", n3);

NSNumber *n4 = [NSNumber numberWithBool:true];
NSLog(@"n4: %@", n4);

NSNumber *n5 = [NSNumber numberWithDouble:100.08];
NSLog(@"n5: %@", n5);

//NSNumber获取
NSLog(@"n2: %c", [n2 charValue]);
NSLog(@"n5: %f", [n5 doubleValue]);

 

NSValue

        //NSValue 初始化 valueWithBytes:<#(const void *)#> objCType:<#(const char *)#>   valueWithRange等
// NSNumber的子类,添加至数组或字典中, 基本类型(NSRect, NSRange, NSSize, NSPoint -> 对象)

NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];

NSRect rect1 = NSMakeRect(3, -10, 30, 20);
NSValue *val1 = [NSValue valueWithRect:rect1];
[array addObject:val1];
NSLog(@"array: %@", array);

NSRange range1 = NSMakeRange(100, 2000);
NSValue *val2 = [NSValue valueWithRange:range1];
[array addObject:val2];
NSLog(@"array: %@", array);

NSSize size1 = NSMakeSize(10, 10);
NSValue *val3 = [NSValue valueWithSize:size1];
[array addObject:val3];
NSLog(@"array: %@", array);

NSPoint point1 = NSMakePoint(30, 50);
NSValue *val4 = [NSValue valueWithPoint:point1];
[array addObject:val4];
NSLog(@"array: %@", array);

NSRect rect2 = NSMakeRect(0, 0, 100, 300);
NSValue *val5 = [NSValue valueWithBytes:&rect2 objCType:@encode(NSRect)];
[array addObject:val5];
NSLog(@"array: %@", array);

NSPoint point2 = NSMakePoint(15, 90);
NSValue *val6 = [NSValue valueWithBytes:&point2 objCType:@encode(NSPoint)];
[array addObject:val6];
NSLog(@"array: %@", array);


//NSValue获取 getValue(要传递得是存储数值的地址) rectValue, pointValue, sizeValue
NSValue *v1 = [array objectAtIndex:0];
NSLog(@"v1: %@", v1);
NSRect rect3;
[v1 getValue:&rect3];
NSLog(@"location: %f, length: %f", rect3.origin.x, rect3.origin.y);

NSRect rect4 = [v1 rectValue];
NSLog(@"width: %f, height: %f", rect4.size.width, rect4.size.height);



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap