一、 KVC
1. KVC
使用前:黯淡无光
if ([keyPath isEqualToString:@"name"]) { self.labelName.text = self.person.name; }else if ([keyPath isEqualToString:@"level"]){ self.labelLevel.text = self.person.level; self.progressViewLabel.progress = [self.person.level floatValue]/6.0; }
使用后:
NSDictionary *labelMap = @{@"name": self.labelName,@"lavel":self.labelLevel}; [labelMap[keyPath] setText:[self.person valueForKeyPath:keyPath]];
2. Key / KeyPath
Key: 你只能KVC 某个对象的直接属性
KeyPath:你可以KVC 对象的属性的属性的属性的.....
3. KVC 什么时候用
1) 配合KVO使用
2) 当你发现如下代码特别多的时候可以使用
self.person.name = node[@"name"]; self.person.phoneNumber = node[@"phoneNumber"]; self.person.resume = node[@"resume"]; self.person.p2 = node[@"p2"]; for(NSString * attribute in node){ [self.person setValue:node[attribute] forKeyPath:attribute]; }
4. KVC 和其它技术融合
Data Binding 数据绑定
self.label
self.person
[self.label bindObject:self.person forKey:@"name"];
二、 Core Data
1. 是什么?
苹果自己使用的数据框架
java: Hibernate
.net: Entity Framework
2. 优势/劣势
优势:处理大型数据模型
劣势:
1. 操作复杂
2. 存储的数据不能跨平台,
只能在iOS以及Mac OS X平台下使用
3. 怎么使用?
1) 创建一个支持Core Data的项目(空项目)
2) 在 Core Data 内增加实体
对比:
nib:
每增加一个界面 就要增加一个xib文件
storyborad:
每增加一个界面 在storyboard中拖拽一个场景
对比:
以前:
每增加一个数据模型 就要创建一个实体类
使用coredata之后:
每增加一个数据模型 在coredata内拖拽一个实体(add entity)添加属性(addAttribute)
3) 生成实体类(选中实体选择editor->creatNSManager...)
1> 生成的类都是继承NSManagedObject
继承这个类是为了协助CoreData管理这个对象
2> 生成的类的.m文件中有
@dynamic
将来的getter和setter的消息实现不由oc语法协助生成
而由其它(CoreData)机制负责生成
3> 生成的类不要勾选options
生成的基础类型都将变为包装类
注意:更改了coreData内的数据后需要重新生成实体类,并在模拟器中kill掉该应用程序。
4) 使用NSManagedObjectContext对数据进行操作
5) 在合适的时候保存
4. 基本操作
1) 增加数据
1> 获取MOC
2> 在MOC中创建对象
3> 利用新对象地址修改对象属性
// 创建模型单例对象ManagedObjectContext MXAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; NSManagedObjectContext *context = appDelegate.managedObjectContext; // 创建实体对象 放入ManagedObjectContext MXContact *contact = [NSEntityDescription insertNewObjectForEntityForName:@"MXContact" inManagedObjectContext:context]; contact.name = self.textFieldName.text; contact.age = @([self.textFieldAge.text intValue]); contact.phoneNumber = self.textFieldPnoneNumber.text; contact.group = self.group; // 增加实体关联
2) 查询数据
1> 获取MOC
2> 创建查询对象
3> 让MOC执行查询
5. 高级查询
1) 可以构建排序器
2) 可以构建筛选器
MXAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; NSManagedObjectContext *context = appDelegate.managedObjectContext; // 创建查询 NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"MXContact"]; // 排序描述器 按姓名排序 升序 NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]; request.sortDescriptors = @[sortDescriptor]; // 刷选 @18是NSNumber对象 // NSString *str = @"18"; // 这里可以从文本框中获取 // NSNumber *number = @([str intValue]); // @()把一个数字转为NSNumber对象 //// request.predicate = [NSPredicate predicateWithFormat:@"age>=%@",@18]; // request.predicate = [NSPredicate predicateWithFormat:@"age>=%@",number]; request.predicate = [NSPredicate predicateWithFormat:@"group.name==%@",self.group.name]; // 如果不排序会返回一个无序的数组 self.contacts = [[context executeFetchRequest:request error:nil] mutableCopy];
6. 改/查
1) 修改对象
1> 拿到对象直接修改属性
2) 删除对象有3步
// 1.删掉模型层的对象 M层 MXAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; NSManagedObjectContext *context = appDelegate.managedObjectContext; [context deleteObject:self.contacts[indexPath.row]]; // 2.删除掉数组中的对象 C层 [self.contacts removeObject:self.contacts[indexPath.row]]; // 3.从界面中移除 V层 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
7. 单实体注意事项
1) 实体名称和类名可以不一样(实体名称最好不要改)
2) 如果给实体增加消息,必须使用Category语法
8. 多实体关联
1) 什么是?
多个数据对象之间的关系 (包含)
2) 关联的种类
方向
1> 双向关联
2> 单向关联
数量
1> To One 对一
2> To Many 对多
一对一 情侣
-> To One
<- To One
一对多 班级 学员
-> To Many
<- To One
多对多 微博 博主
-> To Many
-> To Many
3) 操作
contact.group = .....
//赋值的对象必须已经在MOC中
//而且必须在和contact同样所属的MOC
9. Core Data 初始化数据
必须使用UserDefaults
06-IOSCore - KVC、CoreData,布布扣,bubuko.com
原文:http://www.cnblogs.com/yangmx/p/3583119.html