【问题标题】:how to add core data support ios 9 & ios 10 objective c xcode 8.3如何添加核心数据支持 ios 9 & ios 10 目标 c xcode 8.3
【发布时间】:2017-10-08 17:32:31
【问题描述】:

我使用 Xcode 8.3.2 和核心数据创建了一个新项目。 它在 iOS 10 上运行,但尝试在 iOS 9 上运行时出错。

App delegate 提供核心数据栈并自动保存支持方法。

@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:@"coreDataExample"];
            [_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;
}


- (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();
    }
}

之后我试图保存一些数据,但它给了我错误

由于未捕获的异常“NSInvalidArgumentException”而终止应用,原因:“+entityForName: nil 不是搜索实体名称“FOODCATEGORIES”的合法 NSManagedObjectContext 参数”

这是保存数据的代码

 -(void)addCategoryToDB:(NSArray*)items{
    NSManagedObjectContext *context =  self.persistentContainer.viewContext;
    for(NSInteger i=0;i<items.count;i++){
        NSManagedObject *tmpCategory = [NSEntityDescription insertNewObjectForEntityForName:CORE_DATA_TABLE_CATEGORIES inManagedObjectContext:context];
        FoodCategory* item= [items objectAtIndex:i];
        [tmpCategory setValue:item.name forKey:@"name"];
        [tmpCategory setValue:item.url forKey:@"url"];
        [tmpCategory setValue:item.stripColorCode forKey:@"stripColorCode"];
        [tmpCategory setValue:item.postion forKey:@"position"];
        [tmpCategory setValue:item.buttonImage forKey:@"buttonImage"];
        [self saveContext];
    }
}

【问题讨论】:

  • 尝试从模拟器中删除应用程序。您是否创建了“FOODCATEGORIES”条目?
  • 如果您使用核心数据模型构建应用,然后在实体中进行更改,您需要在设备或模拟器上删除应用,然后重新构建。此外,您可以制作核心数据模型版本,每次更改后,您都需要使用新版本的核心数据进行构建。
  • 方法addCategoryToDB在哪里? self 在该方法中指向什么?如果不是 appDelegate,则 self.persistentContainer 为零。
  • 它在 appDelegate 中,在 iphone 6s 上运行良好
  • 嘿@nazir 它带有新核心数据文件的新应用

标签: objective-c core-data ios9 ios10 xcode8


【解决方案1】:

错误告诉您 self.managedObjectContext 为 nil,这意味着您尚未为该属性分配值。您很可能应该在创建或加载此视图控制器的任何代码中执行此操作(例如,在 prepareForSegue:sender: 中,如果您使用的是故事板)。

以下链接可能对您有所帮助clickHere

【讨论】:

    猜你喜欢
    • 2017-07-12
    • 1970-01-01
    • 2018-07-21
    • 2018-02-25
    • 1970-01-01
    • 2015-12-21
    • 2017-02-13
    • 1970-01-01
    • 2016-01-25
    相关资源
    最近更新 更多