iOS CoreData的使用(包含创建工程时未添加CoreData)
  TL139jY6rCH8 2023年11月02日 31 0

1.在创建工程时未添加CoreData,后期想要使用CoreData则要在工程Appdelegate.h文件中添加CoreData库和CoreData中的通道类(用来管理类实例和CoreData之间的所有操作)和保存到CoreData文件的方法.

iOS CoreData的使用(包含创建工程时未添加CoreData)_实体类

2.添加完这些后去创建.xcdatamodeld文件

iOS CoreData的使用(包含创建工程时未添加CoreData)_实体类_02

3.填写创建文件的名称(建议与工程名字一致后面添加CoreData)

iOS CoreData的使用(包含创建工程时未添加CoreData)_#pragma_03

4.文件创建完成后就可以像以前一样去创建对应实体文件和添加实体的属性了.

iOS CoreData的使用(包含创建工程时未添加CoreData)_实体类_04

5.添加完实体后生成对应的实体类文件

iOS CoreData的使用(包含创建工程时未添加CoreData)_Core_05

6.创建完对应的实体类文件后回到Appdelegate.m中去实现添加的方法和实例

1.首先在Appdelegate.m的- (void)applicationWillTerminate:(UIApplication *)application;中调用下保存的方法.

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    [self saveContext];
}

2.然后实现通道类和保存的方法.

#pragma mark - Core Data stack

@synthesize persistentContainer = _persistentContainer;

- (NSPersistentContainer *)persistentContainer {
    // The persistent container for the application. This implementation creates and returns a container, having loaded the store for the application to it.
    @synchronized (self) {
        if (_persistentContainer == nil) {
            _persistentContainer = [[NSPersistentContainer alloc] initWithName:@"LotterSelectCoreData"];
            [_persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *storeDescription, NSError *error) {
                if (error != nil) {
                    // 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.
                    
                    /*
                     Typical reasons for an error here include:
                     * The parent directory does not exist, cannot be created, or disallows writing.
                     * The persistent store is not accessible, due to permissions or data protection when the device is locked.
                     * The device is out of space.
                     * The store could not be migrated to the current model version.
                     Check the error message to determine what the actual problem was.
                     */
                    NSLog(@"Unresolved error %@, %@", error, error.userInfo);
                    abort();
                }
            }];
        }
    }
    
    return _persistentContainer;
}

#pragma mark - Core Data Saving support

- (void)saveContext {
    NSManagedObjectContext *context = self.persistentContainer.viewContext;
    NSError *error = nil;
    if ([context hasChanges] && ![context 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();
    }
}
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  hAj4qcBP7pV1   2023年11月19日   104   0   0 AppCoreiosAppCoreios
  xEIKQOiGayQx   2023年11月02日   114   0   0 DataCoreswiftCoreswiftData
TL139jY6rCH8