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

首頁 > 系統 > iOS > 正文

iOS Xcode8下CoreData的簡單應用

2019-11-09 16:33:05
字體:
來源:轉載
供稿:網友

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

預備工作

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

2.然后就會發現工程里多出來一個這樣的文件: 這里寫圖片描述

3.然后這樣搞一下: 應該很清楚吧

在之前的Xcode版本里,這時候應該選擇Xcode工具欄中的Editor->Create NSManagedObject Subclass…,從而產生實體相對應的類,但是在Xcode8中,不用了!有沒有很開心???如果你又多操作了這一步,會報錯的,親身爬坑體會。

具體操作

1.添加數據

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中的數據添加到數據源中:

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(@"數據查詢錯誤%@",error); }else{ //將查詢到的數據添加到數據源中 [self.dataArray addObjectsFromArray:fetchedObjects]; }

2.刪除數據

//滑動后紅色刪除按鈕上顯示的文字-(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中的數據 [self.appdelegate.persistentContainer.viewContext deleteObject:person]; //持久化一下 [self.appdelegate saveContext]; }}

3.修改數據

if ([person.sex isEqualToString:@"男"]) { person.sex = @"女"; person.name = @"新名字"; [self.appDelegate saveContext]; }

4.查詢數據

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(@"數據查詢錯誤%@",error); }else{ //查詢到之后要你的操作代碼 [self.dataArray removeAllObjects]; [self.dataArray addObjectsFromArray:fetchedObjects]; [self.tableView reloadData]; }

以上就是CoreData的增刪改查操作,以前習慣了SQLite的使用,乍一用CoreData,覺得比SQLite還簡便。附上demo地址: https://github.com/ZJQian/CoreDataDemo/tree/master

共同學習,共同進步!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 安宁市| 新巴尔虎右旗| 东山县| 巫溪县| 澄江县| 桓台县| 开鲁县| 长岛县| 项城市| 宜君县| 奇台县| 涞水县| 万全县| 井陉县| 额尔古纳市| 泰兴市| 安西县| 福泉市| 东辽县| 和硕县| 广昌县| 永宁县| 沭阳县| 沾化县| 耿马| 新兴县| 叶城县| 诸暨市| 濉溪县| 黄山市| 平遥县| 同德县| 措美县| 汉源县| 深圳市| 砀山县| 沙湾县| 长汀县| 潜江市| 城市| 和田市|