日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

02-初识CoreData

發(fā)布時(shí)間:2024/9/30 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 02-初识CoreData 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.


1.1-如何給應(yīng)用添加CoreData

  • 創(chuàng)建一個(gè)iOS App的時(shí)候,勾選下方的Use CoreData

1.2-應(yīng)用添加了CoreData之后發(fā)生了哪些變化?



  • (1)Bunlde文件中多了一個(gè)數(shù)據(jù)模型文件

    • 該文件的主要作用是可以直觀的用界面來處理數(shù)據(jù)

  • (2)AppDelegate文件中多了一堆代碼

    • 該代碼由Xcode自動(dòng)生成,作用是搭建使用CoreData的環(huán)境
#pragma mark - Core Data stack@synthesize managedObjectContext = _managedObjectContext; @synthesize managedObjectModel = _managedObjectModel; @synthesize persistentStoreCoordinator = _persistentStoreCoordinator;- (NSURL *)applicationDocumentsDirectory {// The directory the application uses to store the Core Data store file. This code uses a directory named "itheima.CoreData___" in the application's documents directory.return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; }- (NSManagedObjectModel *)managedObjectModel {// The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.if (_managedObjectModel != nil) {return _managedObjectModel;}NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"CoreData___" withExtension:@"momd"];_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];return _managedObjectModel; }- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {// The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added the store for the application to it.if (_persistentStoreCoordinator != nil) {return _persistentStoreCoordinator;}// Create the coordinator and store_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreData___.sqlite"];NSError *error = nil;NSString *failureReason = @"There was an error creating or loading the application's saved data.";if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {// Report any error we got.NSMutableDictionary *dict = [NSMutableDictionary dictionary];dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";dict[NSLocalizedFailureReasonErrorKey] = failureReason;dict[NSUnderlyingErrorKey] = error;error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];// Replace this with code to handle the error appropriately.// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.NSLog(@"Unresolved error %@, %@", error, [error userInfo]);abort();}return _persistentStoreCoordinator; }- (NSManagedObjectContext *)managedObjectContext {// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)if (_managedObjectContext != nil) {return _managedObjectContext;}NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];if (!coordinator) {return nil;}_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];[_managedObjectContext setPersistentStoreCoordinator:coordinator];return _managedObjectContext; }#pragma mark - Core Data Saving support- (void)saveContext {NSManagedObjectContext *managedObjectContext = self.managedObjectContext;if (managedObjectContext != nil) {NSError *error = nil;if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {// Replace this implementation with code to handle the error appropriately.// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.NSLog(@"Unresolved error %@, %@", error, [error userInfo]);abort();}} }

1.3-如何使用CoreData創(chuàng)建數(shù)據(jù)?

  • 在模型文件中添加一個(gè)實(shí)體Entity(以Person為例),并且給Person實(shí)體添加兩個(gè)屬性(Attribute) 一個(gè)是名字name一個(gè)是年齡age


  • 給模型文件中的Person實(shí)體添加一個(gè)對(duì)應(yīng)的Person文件


  • 創(chuàng)建成功之后可以看到工程多了四個(gè)文件,他們的作用主要是根據(jù)模型文件生成對(duì)應(yīng)的OC對(duì)象


  • 在ViewController文件中保存Person的數(shù)據(jù)





  • 通過打印應(yīng)用程序的沙盒路徑和Bundle路徑,我們可以看到生成的數(shù)據(jù)庫(kù)文件是保存在沙盒文件中的





  • 通過Navicat Permium軟件打開生成的sqlite文件可以看到Person表格中添加了一行數(shù)據(jù)





1.4-總結(jié):CoreDate是如何將數(shù)據(jù)庫(kù)處理成OC對(duì)象的?





數(shù)據(jù)庫(kù)的一張表對(duì)應(yīng)的是CoreData模型文件中的一個(gè)實(shí)體Entity數(shù)據(jù)庫(kù)表的列對(duì)應(yīng)的是CoreData模型文件中實(shí)體Entity的屬性Attribute數(shù)據(jù)庫(kù)表的每一行對(duì)應(yīng)的是我們剛剛用代碼創(chuàng)建的一個(gè)Person對(duì)象

總結(jié)

以上是生活随笔為你收集整理的02-初识CoreData的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。