原來沒怎么使用過CoreData,最近心血來潮使用了一下,簡直是很好用啊。。。相比于之前的Xcode版本,Xcode8下的CoreData又簡單了不少。。。
1.新建一個(gè)工程:
這里不要忘記打上勾哦!
2.然后就會發(fā)現(xiàn)工程里多出來一個(gè)這樣的文件: 
3.然后這樣搞一下: 
在之前的Xcode版本里,這時(shí)候應(yīng)該選擇Xcode工具欄中的Editor->Create NSManagedObject Subclass…,從而產(chǎn)生實(shí)體相對應(yīng)的類,但是在Xcode8中,不用了!有沒有很開心???如果你又多操作了這一步,會報(bào)錯(cuò)的,親身爬坑體會。
1.添加數(shù)據(jù)
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.appdelegate.persistentContainer.viewContext]; Person *person = [[Person alloc]initWithEntity:entityDescription insertIntoManagedObjectContext:self.appdelegate.persistentContainer.viewContext]; person.name = [NSString stringWithFormat:@"JACK%d",arc4random()%100]; person.age = arc4random()%60+1; person.sex = arc4random()%2==0?@"man":@"woman"; [self.dataArray insertObject:person atIndex:0]; [self.appdelegate saveContext]; [self.tableView reloadData];只寫這些東西會crash的,需要在viewdidload中,將coreData中的數(shù)據(jù)添加到數(shù)據(jù)源中:
self.appdelegate = (AppDelegate *)[UIapplication sharedApplication].delegate; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.appdelegate.persistentContainer.viewContext]; [fetchRequest setEntity:entity]; // Specify criteria for filtering which objects to fetch // NSPRedicate *predicate = [NSPredicate predicateWithFormat:@"age = 21", ]; // [fetchRequest setPredicate:predicate]; // Specify how the fetched objects should be sorted NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES]; [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]]; NSError *error = nil; NSArray *fetchedObjects = [self.appdelegate.persistentContainer.viewContext executeFetchRequest:fetchRequest error:&error]; if (fetchedObjects == nil) { NSLog(@"數(shù)據(jù)查詢錯(cuò)誤%@",error); }else{ //將查詢到的數(shù)據(jù)添加到數(shù)據(jù)源中 [self.dataArray addObjectsFromArray:fetchedObjects]; }2.刪除數(shù)據(jù)
//滑動后紅色刪除按鈕上顯示的文字-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{ return @"刪除";}-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ //刪除情況下 if (editingStyle == UITableViewCellEditingStyleDelete) { Person *person = self.dataArray[indexPath.row]; [self.dataArray removeObject:person]; [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; //刪除CoreData中的數(shù)據(jù) [self.appdelegate.persistentContainer.viewContext deleteObject:person]; //持久化一下 [self.appdelegate saveContext]; }}3.修改數(shù)據(jù)
if ([person.sex isEqualToString:@"男"]) { person.sex = @"女"; person.name = @"新名字"; [self.appDelegate saveContext]; }4.查詢數(shù)據(jù)
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.appdelegate.persistentContainer.viewContext]; [fetchRequest setEntity:entity]; // Specify criteria for filtering which objects to fetch //謂詞搜索 // NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age = 21", ]; // [fetchRequest setPredicate:predicate]; // Specify how the fetched objects should be sorted //排序方法(這里為按照年齡升序排列) NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES]; [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]]; NSError *error = nil; NSArray *fetchedObjects = [self.appdelegate.persistentContainer.viewContext executeFetchRequest:fetchRequest error:&error]; if (fetchedObjects == nil) { NSLog(@"數(shù)據(jù)查詢錯(cuò)誤%@",error); }else{ //查詢到之后要你的操作代碼 [self.dataArray removeAllObjects]; [self.dataArray addObjectsFromArray:fetchedObjects]; [self.tableView reloadData]; }以上就是CoreData的增刪改查操作,以前習(xí)慣了SQLite的使用,乍一用CoreData,覺得比SQLite還簡便。附上demo地址: https://github.com/ZJQian/CoreDataDemo/tree/master
共同學(xué)習(xí),共同進(jìn)步!
新聞熱點(diǎn)
疑難解答
圖片精選