【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,修改

最新文章

  1. 两个list 合并成新一个list
  2. python求范数
  3. POJ 1830 高斯消元
  4. Java面向对象三大特点之继承
  5. cdr创建样式与样式集的方法
  6. 定时重启Apache与MySQL方法
  7. 【Zookeeper学习】Apache Zookeeper项目简介
  8. docker build lnmp(未完成。。。)
  9. 让IE的Button自适应文字宽度兼容
  10. iOS tableview和 Collection复用机制
  11. 1.2 Python开发环境
  12. Servlet线程
  13. Apache Hadoop学习笔记一
  14. RMAN命令DELETE操作总结
  15. P1654 OSU!-洛谷luogu
  16. Java主要版本平台
  17. Python 入门基础16 -- ATM + 购物车
  18. JSON字符串转C#实体Class类
  19. OC 构造方法(对象初始化)
  20. 虚拟键盘 input 挡住

热门文章

  1. C++ Primer的课后规划问题的第八章
  2. Java thread中对异常的处理策略
  3. Spring AOP基于xml配置实例
  4. 建立dblink,clob
  5. Aptana Studio 3 官方汉化包汉化
  6. 红帽系列linux自行配置本地yum源
  7. C语言循环小技巧
  8. hdu 1875 畅通project再续
  9. android 时间滚动控件 底部弹出
  10. Android基调(十六)- Service:startService()、stopService()、bindService()、unbindService()加