【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]; // 保存数据 [app saveContext];
从数据库查询数据:
// 创建查询请求 NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Person"]; // 设置查询条件 [request setPredicate:[NSPredicate predicateWithFormat:@"name=‘李四‘&&age>13"]]; // 设置排序 升序 [request setSortDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES]]]; // 执行查询请求 并返回结果 NSArray *personArray = [app.managedObjectContext executeFetchRequest:request error:nil]; // 数组的排序 personArray = [personArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { Person *p1 = obj1; Person *p2 = obj2; if (p1.age.intValue < p2.age.intValue) { return NSOrderedAscending; // 升序 }else{ return NSOrderedDescending; // 降序 } }]; // 输出 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]; [app saveContext];
【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 } }
集合转数组的方法
self.playerArray = self.teamObj.players.allObjects; // 集合转换为数组
// 去除字符串两端空格
name = [name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
alert输入框内容发生改变时调用此方法
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
【day06_team&player】:多实体之间的关系
如果一个实体和另一个实体要建立关系的话,在style显示模式下按住ctrl键连线到另一个实体上,要建立一对多关系选中newRelationships设置type为to many
切记!如果项目中用到了CoreData,在生成过实体对象之后运行过程序 并且在之后修改了实体对象必须把模拟器里的项目删除掉
测试代码:
- (void)viewDidLoad { [super viewDidLoad]; // 添加球队 MXAppDelegate *app = [UIApplication sharedApplication].delegate; // Team *team = [NSEntityDescription insertNewObjectForEntityForName:@"Team" inManagedObjectContext:app.managedObjectContext]; // team.name = @"湖人"; // [app saveContext]; NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Team"]; NSArray *teams = [app.managedObjectContext executeFetchRequest:request error:Nil]; for (Team *t in teams) { if ([t.name isEqualToString:@"湖人"]) { // 添加球员 Player *player = [NSEntityDescription insertNewObjectForEntityForName:@"Player" inManagedObjectContext:app.managedObjectContext]; player.name = @"科比3"; player.myTeam = t; [app saveContext]; } NSLog(@"球队:%@ %d人",t.name,t.myPlayers.count); for (Player *p in t.myPlayers) { NSLog(@"%@",p.name); } } }
总结:使用数据库完成增删改要保存数据,查不需要。
综合练习:
打飞机游戏需求,
1.第一次运行直接进到添加用户的页面
2.从添加用户页面 选择某一个用户的话,返回首页,并显示欢迎这个用户
3.点击排行榜,跳转到新的页面,此页面显示每一个用户的最高分
4.用户列表页面可以进行删除和添加用户
5.将本次游戏得分和当前用户的最高分做比较如果这次高就覆盖得分
【PlayPlaneGame】:打飞机游戏
案例总结:如果要对从数据库取出的数组进行操作,应该让该数组为可变数组。
修改快捷键使用command+,打开,选择key bindings,修改
原文:http://www.cnblogs.com/yangmx/p/3561257.html