Ios 更新核心数据模型后,我收到一个;“无法识别的选择器”;错误

Ios 更新核心数据模型后,我收到一个;“无法识别的选择器”;错误,ios,objective-c,core-data,nsmanagedobjectmodel,Ios,Objective C,Core Data,Nsmanagedobjectmodel,我有核心数据模型,它工作得很好,但我需要添加新属性。我单击我的.xcdatamodel并转到编辑器/添加模型版本。现在我添加了新属性,并将其添加到.h和.m文件中 当我运行应用程序时,它会给我一个错误: [CubeCategory setStoreDescription:]: unrecognized selector sent to instance 0x1657bca0 现在,即使我从设备中删除应用程序,从fresh中删除instal,它仍然会给我同样的错误 我做错了什么 编辑: 我已将我

我有核心数据模型,它工作得很好,但我需要添加新属性。我单击我的.xcdatamodel并转到编辑器/添加模型版本。现在我添加了新属性,并将其添加到.h和.m文件中

当我运行应用程序时,它会给我一个错误:

[CubeCategory setStoreDescription:]: unrecognized selector sent to instance 0x1657bca0
现在,即使我从设备中删除应用程序,从fresh中删除instal,它仍然会给我同样的错误

我做错了什么

编辑:

我已将我的新型号设置为当前型号:

我的模型看起来像:

这个类看起来像:

.h:

还有,m

@implementation CubeCategory

@dynamic categoryID;
@dynamic position;
@dynamic title;
@dynamic type;
@dynamic cube;
@dynamic server;
@dynamic lock;
@dynamic storeDescription;

-(id)init:(NSManagedObjectContext*)Context{
    self = [NSEntityDescription insertNewObjectForEntityForName:@"CubeCategory" inManagedObjectContext:Context];
    return self;
}

-(void)save:(NSManagedObjectContext*)Context{
    NSError *error;
    if (![Context save:&error]) {
        NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
    }
}


@end
在我的AppDelegate中,我设置了:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    NSDictionary *options = @{
                              NSMigratePersistentStoresAutomaticallyOption : @YES,
                              NSInferMappingModelAutomaticallyOption : @YES
                              };
 ...

     if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
     }
编辑2:

- (NSManagedObjectModel *)managedObjectModel
{
    if (__managedObjectModel != nil) {
        return __managedObjectModel;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"mom"];
    __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    return __managedObjectModel;
}

// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    NSDictionary *options = @{
                          NSMigratePersistentStoresAutomaticallyOption : @YES,
                          NSInferMappingModelAutomaticallyOption : @YES
                          };


    if (__persistentStoreCoordinator != nil) {
        return __persistentStoreCoordinator;
    }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"BiViewNew.sqlite"];

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

    return __persistentStoreCoordinator;
}

您需要使用创建persistentStoreCoordinator

 NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                         [NSNumber numberWithBool:YES],    NSMigratePersistentStoresAutomaticallyOption,
                         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

还有。您是否使用编辑器/create NSManagedObject子类创建了更新的托管对象

为了确保旧版本的verison与新版本的核心数据模型之间的轻迁移,您必须执行以下步骤:

1-添加模型版本我想你已经这么做了 2-设置UIManagedDocument的persistentStoreOptions,如下所示

   NSDictionary *options = @{
                              NSMigratePersistentStoresAutomaticallyOption : @YES,
                              NSInferMappingModelAutomaticallyOption : @YES
                              };
    self.myAppiPhoneDatabase.persistentStoreOptions = options;

理想情况下,打开或创建文档时必须设置persistentStoreOptions(persistentStoreOptions)

我忘记了合成新变量(
例如:customColor
,在模型
中添加了NSObject和NSManagedObject
,在我的例子中,我同时使用了这两个变量)使用导致崩溃的
@synthesis
。如果我们没有合成变量,那么在访问它时需要setter方法,这是我崩溃的根源。它可能会帮助某人


注意:在我的情况下,这不是迁移问题。

我已经设置了选项,并且我已经更新了托管对象:右键单击/new file/core data/NSManagedObject子类,我假设它与您的相同。在数据建模器中选择您的实体,然后转到菜单编辑器。在那里创建对象表单-“创建NSManagedObject子类”同样,您是否为整个数据模型选择了新的模型版本作为当前版本(右侧的标识和类型属性面板)是否在创建新版本后选择了新版本作为当前版本?如果使用NSManagedObject子类,请确保将这些属性添加到托管对象子类中,否则请使用kvc访问实体上的属性。是的,我选择新版本作为当前版本能否显示模型和/或代码?如果
CubeCategory
是一个实例,则应将其称为
CubeCategory
。遵循命名约定…一切看起来都很好。确保你的上下文是正确的。这意味着您的persistentStoreCoordinator和managedObjectModel应该设置正确。我不确定您的意思。我编辑我的问题并添加创建这些对象的函数。我像addPersistentStoreWithType:NSSQLiteStoreType配置:nil URL:storeURL选项:选项错误:&error;选项是你的字典
   NSDictionary *options = @{
                              NSMigratePersistentStoresAutomaticallyOption : @YES,
                              NSInferMappingModelAutomaticallyOption : @YES
                              };
    self.myAppiPhoneDatabase.persistentStoreOptions = options;