1

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

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

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

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

我做错了什么?

编辑 :

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

在此处输入图像描述

我的模型看起来像:

在此处输入图像描述

和类看起来像:

。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;
}
4

3 回答 3

0

您需要使用创建 persistentStoreCoordinator

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

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

于 2014-05-13T11:15:38.530 回答
0

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

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

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

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

于 2014-05-13T11:27:08.310 回答
0

我忘记合成新变量( ,在我使用两者的情况下eg: customColor添加到模型中)使用它会导致崩溃。如果我们没有合成变量,我们在访问它时需要 setter 方法,这是我崩溃的根源。它可能会帮助某人。NSObject and NSManagedObject@synthesize

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

于 2016-07-26T13:58:43.967 回答