更新Core Data模型后,我收到了一个"无法识别的选择器"错误

时间:2014-05-13 11:06:57

标签: ios objective-c core-data nsmanagedobjectmodel

我有Core Data模型,它工作正常,但我需要添加新属性。我点击我的.xcdatamodel并转到编辑/添加模型版本。现在我添加新属性并将其添加到.h和.m文件中。

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

[CubeCategory setStoreDescription:]: unrecognized selector sent to instance 0x1657bca0

现在,即使我从设备中删除应用程序并从新设备安装它仍然给我同样的错误。

我做错了什么?

编辑:

我已将新模型设为当前模型:

enter image description here

我的模型看起来像:

enter image description here

Class看起来像:

.h:

@interface CubeCategory : NSManagedObject

@property (nonatomic, retain) NSNumber * categoryID;
@property (nonatomic, retain) NSNumber * position;
@property (nonatomic, retain) NSString * title;
@property (nonatomic, retain) NSNumber * type;
@property (nonatomic, retain) NSString * storeDescription;
@property (nonatomic, retain) NSNumber * lock;
@property (nonatomic, retain) NSSet *cube;
@property (nonatomic, retain) Server *server;
@end

@interface CubeCategory (CoreDataGeneratedAccessors)

- (void)addCubeObject:(Cube *)value;
- (void)removeCubeObject:(Cube *)value;
- (void)addCube:(NSSet *)values;
- (void)removeCube:(NSSet *)values;

-(id)init:(NSManagedObjectContext*)Context;
-(void)save:(NSManagedObjectContext*)Context;

@end

和.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;
}

3 个答案:

答案 0 :(得分:0)

您需要使用

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

另外。您是否使用编辑器/创建NSManagedObject子类

创建更新的托管对象

答案 1 :(得分:0)

要确保先前版本和新版Core Data模型之间的轻微迁移,您必须执行以下步骤:

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

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

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

答案 2 :(得分:0)

我忘了合成新变量( eg: customColor ,在我的案例中添加模型 NSObject and NSManagedObject 我同时使用这两个变量使用 @synthesize 进行崩溃。如果我们没有合成变量,我们在访问它时需要setter方法,这是我崩溃的根源。这可能对某人有帮助。

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