【问题标题】:Migrate core data to add new attribute迁移核心数据以添加新属性
【发布时间】:2015-08-07 00:09:27
【问题描述】:

我添加了新属性,我需要迁移核心数据。结果,我喜欢这样。

Bus.xcDataModel >> 添加模型版本

然后,我添加新属性。我也像这样更改持久存储协调器选项。

@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}

但是当我添加那个新属性时,它会像这样显示我。我该怎么办?

-[BusService setBus_wap:]:无法识别的选择器发送到实例 0x1742a7320

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator_busservice
{
    if (_persistentStoreCoordinator_busservice != nil)
        return _persistentStoreCoordinator_busservice;

    NSURL *applicationDocumentsDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    NSURL *storeURL = [applicationDocumentsDirectory URLByAppendingPathComponent:@"Busservice_new.sqlite"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:[storeURL path]] &&
    !self.preloadEnabled)
    {
        NSURL *preloadURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Busservice_new" ofType:@"sqlite"]];

        NSError* err = nil;

    if (![[NSFileManager defaultManager] copyItemAtURL:preloadURL toURL:storeURL error:&err])
        DLog(@"Oops, could not copy preloaded data");
}

    NSError *error = nil;
_persistentStoreCoordinator_busservice = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![_persistentStoreCoordinator_busservice addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES} error:&error])
{
    DLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

return _persistentStoreCoordinator_busservice;
}

【问题讨论】:

    标签: core-data core-data-migration


    【解决方案1】:

    从该错误看来,您向 Core Data 中的 BusService 实体添加了一个新属性,但没有将该属性添加到您的 BusService 类。如果你想对这个新属性使用访问器方法,你也需要更新类。或者您可以不理会该类,而是使用setValue:forKey: 为新属性分配值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-11
      • 1970-01-01
      • 2015-03-07
      • 1970-01-01
      • 2011-09-14
      • 2017-07-07
      • 2011-04-07
      • 2021-05-07
      相关资源
      最近更新 更多