您的位置:首页 > 产品设计 > UI/UE

黑马程序员--NSNumber NSValue

2015-10-17 12:57 330 查看
-----------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(100, 50);//包装size类型

NSPoint p = CGPointMake(0, 0);

NSRect rect = CGRectMake(0, 0, 100, 50);

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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: