高级UIKit-05(CoreData)
【day06_1_CoreDataPerson】:保存person对象到coreData数据库
保存大量数据时用CoreData保存到数据库,数据库会存在documents目录下
操作步骤:
1.创建空项目,勾上coreData
2.选中day06_1_CoreDataPerson.xcdatamo
添加entity实体,添加属性(attributes)
interger 16 int类型
interger 32 long类型
interger 16 long long类型
3.创建实体类,选择coreData选择最后一个,下一步。。。
4.创建storyboard,选择interface选中storyboard
5.选中项目名称选择main interface选中storyboard,删掉MXAppDelegate.m中创建window的代码,这时界面才能显示出来
添加数据到数据库:
// 创建appDelegate对象
MXAppDelegate *app = [UIApplication sharedApplication].delegate;
// 创建Person对象
Person *p = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:app.managedObjectContext];
p.name = @"李四";
p.age = [NSNumber numberWithInt:12];
从数据库查询数据:
// 创建appDelegate对象
MXAppDelegate *app = [UIApplication sharedApplication].delegate;
// 创建查询请求
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Person"];
// 执行查询请求 并返回结果
NSArray *personArray = [app.managedObjectContext executeFetchRequest:request error:nil];
// 输出
for (Person *p in personArray) {
NSLog(@"%@,%@",p.name,p.age);
}
删除数据:
// 创建appDelegate对象
MXAppDelegate *app = [UIApplication sharedApplication].delegate;
// 创建查询请求
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Person"];
// 执行查询请求 并返回结果
NSArray *personArray = [app.managedObjectContext executeFetchRequest:request error:nil];
for (Person *p in personArray) {
if ([p.name isEqualToString:@"张三"]) {
[app.managedObjectContext deleteObject:p]; // 删除数据
}
}
修改数据:
// 创建appDelegate对象
MXAppDelegate *app = [UIApplication sharedApplication].delegate;
// 创建查询请求
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Person"];
// 执行查询请求 并返回结果
NSArray *personArray = [app.managedObjectContext executeFetchRequest:request error:nil];
Person *p = personArray[0];
p.name = @"王五";
p.age = [NSNumber numberWithInt:23];
【day06_2_CoreDataTeam】:综合案例,使用数据库完成增删改查功能
添加和修改:
// 点击按钮后添加或修改数据
- (IBAction)clickedAction:(id)sender {
if (self.t) {
self.t.name = self.nameTF.text;
self.t.location = self.locationTF.text;
}else{
// 创建插入数据库对象
Team *team = [NSEntityDescription insertNewObjectForEntityForName:@"Team" inManagedObjectContext:self.app.managedObjectContext];
team.name = self.nameTF.text;
team.location = self.locationTF.text;
}
[self.app saveContext];
[self.navigationController popViewControllerAnimated:YES];
}
删除数据:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
Team *t = self.teamArray[indexPath.row];
[self.app.managedObjectContext deleteObject:t]; // 删除
[self.app saveContext]; // 保存
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
总结:使用数据库完成增删改要保存数据,查不需要。
综合练习:
游戏需求,
1.第一次运行直接进到添加用户的页面
2.从添加用户页面 选择某一个用户的话,返回首页,并显示欢迎这个用户
3.点击排行榜,跳转到新的页面,此页面显示每一个用户的最高分
4.用户列表页面可以进行删除和添加用户
5.将本次游戏得分和当前用户的最高分做比较如果这次高就覆盖得分
【PlayPlaneGame】:打飞机游戏
案例总结:如果要对从数据库取出的数组进行操作,应该让该数组为可变数组。
修改快捷键使用command+,打开,选择key bindings,修改
最新文章
- 两个list 合并成新一个list
- python求范数
- POJ 1830 高斯消元
- Java面向对象三大特点之继承
- cdr创建样式与样式集的方法
- 定时重启Apache与MySQL方法
- 【Zookeeper学习】Apache Zookeeper项目简介
- docker build lnmp(未完成。。。)
- 让IE的Button自适应文字宽度兼容
- iOS tableview和 Collection复用机制
- 1.2 Python开发环境
- Servlet线程
- Apache Hadoop学习笔记一
- RMAN命令DELETE操作总结
- P1654 OSU!-洛谷luogu
- Java主要版本平台
- Python 入门基础16 -- ATM + 购物车
- JSON字符串转C#实体Class类
- OC 构造方法(对象初始化)
- 虚拟键盘 input 挡住
热门文章
- C++ Primer的课后规划问题的第八章
- Java thread中对异常的处理策略
- Spring AOP基于xml配置实例
- 建立dblink,clob
- Aptana Studio 3 官方汉化包汉化
- 红帽系列linux自行配置本地yum源
- C语言循环小技巧
- hdu 1875 畅通project再续
- android 时间滚动控件 底部弹出
- Android基调(十六)- Service:startService()、stopService()、bindService()、unbindService()加