国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

iOS Xcode8下CoreData的簡單應(yīng)用

2019-11-09 17:36:40
字體:
供稿:網(wǎng)友

原來沒怎么使用過CoreData,最近心血來潮使用了一下,簡直是很好用啊。。。相比于之前的Xcode版本,Xcode8下的CoreData又簡單了不少。。。

預(yù)備工作

1.新建一個(gè)工程: 這里寫圖片描述 這里不要忘記打上勾哦!

2.然后就會發(fā)現(xiàn)工程里多出來一個(gè)這樣的文件: 這里寫圖片描述

3.然后這樣搞一下: 應(yīng)該很清楚吧

在之前的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)步!


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 安国市| 常宁市| 米脂县| 台北县| 黑河市| 措勤县| 曲水县| 石阡县| 凤阳县| 临泉县| 漳州市| 桑植县| 兴安县| 玉龙| 敦煌市| 屏边| 津市市| 汾阳市| 阿荣旗| 新巴尔虎右旗| 德安县| 棋牌| 英德市| 双辽市| 大厂| 舒兰市| 龙里县| 方山县| 夹江县| 吉安市| 周口市| 南安市| 分宜县| 保定市| 泸定县| 河北区| 北流市| 扶风县| 扎赉特旗| 临泽县| 天津市|