黑马程序员--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);
-----------并不是生来就是大神、代码量决定一切、其他都是假象、-----------
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);
相关文章推荐
- 【UIKit-33-1】UIDatePicker - 时间选择器
- uiTableView 默认选中并加载数据
- StringBuilder和StringBuffer的区别
- AlertView 和 UIAlertController 简析学习和简单示例
- 【Mockplus教程】如何批量修改组件属性
- 【Mockplus教程】如何使用本地图片和在线图片
- 【Mockplus教程】为什么我不能设置链接的切换动画?
- 翻译:深入 AngularUI Router
- IOS笔记UI--Button设置选中字体颜色
- MySQL(18):Select- subquery子查询
- UITableView常见用法以及注意细节
- UI -NSBundle
- SPOJ GSS1 Can you answer these queries I
- autolayout UIImageView 根据 UILabel的宽度变换位置
- MFC的CreateTimerQueueTimer和timeSetEvent两个高精度定时器的比较
- Queue类
- SPOJ GSS7 Can you answer these queries VII
- 利用运行时设置UITextField 光标颜色
- iOS界面编程-UIButton
- Difference between Key, Primary Key, Unique Key and Index in MySQL