iOS-轻量级迁移-无法识别的选择器

iOS-轻量级迁移-无法识别的选择器,ios,core-data,Ios,Core Data,我正在尝试进行轻量级迁移。我一步一步地做了一个新的跟随者,我开始工作了。但现在我在另一个应用程序中,一切似乎都很顺利,但当我尝试访问新字段时,我得到以下错误 [Promocion setEnlace:]: unrecognized selector sent to instance 0x7fafe40bceb0 我选择了正确的型号。 这是模型促销 #import <Foundation/Foundation.h> #import <CoreData/CoreData.h&g

我正在尝试进行轻量级迁移。我一步一步地做了一个新的跟随者,我开始工作了。但现在我在另一个应用程序中,一切似乎都很顺利,但当我尝试访问新字段时,我得到以下错误

[Promocion setEnlace:]: unrecognized selector sent to instance 0x7fafe40bceb0
我选择了正确的型号。

这是模型促销

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

/*
@interface Promocion : NSManagedObject

@property (nonatomic, retain) NSNumber * id_promocion;
@property (nonatomic, retain) NSString * descripcion;
@property (nonatomic, retain) NSString * titulo;
@property (nonatomic, retain) NSString * url;
@property (nonatomic, retain) NSString * fecha_inicio;
@property (nonatomic, retain) NSString * fecha_fin;

@end
 */

NS_ASSUME_NONNULL_BEGIN

@interface Promocion : NSManagedObject

// Insert code here to declare functionality of your managed object subclass

//+(id)personaWithContext:(NSManagedObjectContext *)context;

@end

NS_ASSUME_NONNULL_END

#import "Promocion+CoreDataProperties.h"
以下是自动生成的文件 Promocion+CoreDataProperties.h

#import "Promocion.h"

NS_ASSUME_NONNULL_BEGIN

@interface Promocion (CoreDataProperties)

@property (nullable, nonatomic, retain) NSString *descripcion;
@property (nullable, nonatomic, retain) NSString *fecha_fin;
@property (nullable, nonatomic, retain) NSString *fecha_inicio;
@property (nullable, nonatomic, retain) NSNumber *id_promocion;
@property (nullable, nonatomic, retain) NSString *titulo;
@property (nullable, nonatomic, retain) NSString *url;
@property (nullable, nonatomic, retain) NSString *enlace;

@end

NS_ASSUME_NONNULL_END
@implementation Promocion (CoreDataProperties)

@dynamic descripcion;
@dynamic fecha_fin;
@dynamic fecha_inicio;
@dynamic id_promocion;
@dynamic titulo;
@dynamic url;
@dynamic enlace;

@end
Promocion+CoreDataProperties.m #导入“Promocion+CoreDataProperties.h”

AppDelegate.m

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }

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

    //Migration
    NSDictionary *options = @{
                              NSMigratePersistentStoresAutomaticallyOption  :@YES,
                              NSInferMappingModelAutomaticallyOption        :@YES
                              };

    NSError *error = nil;
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                                   configuration:nil
                                                             URL:storeURL
                                                        //options:nil
                                                         options:options//automatic migration
                                                           error:&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.

         Typical reasons for an error here include:
         * The persistent store is not accessible;
         * The schema for the persistent store is incompatible with current managed object model.
         Check the error message to determine what the actual problem was.


         If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.

         If you encounter schema incompatibility errors during development, you can reduce their frequency by:
         * Simply deleting the existing store:
         [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]

         * Performing automatic lightweight migration by passing the following dictionary as the options parameter:
         @{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}

         Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.

         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        //abort();
    }

    return _persistentStoreCoordinator;
}
附加信息 我已经更新了项目的版本

在这里,我访问名为“enlace”的字段 我试过的 我已经更改了Promocion.h以避免使用Promocion+CoreDataProperties.h,但仍然存在相同的错误

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@interface Promocion : NSManagedObject

@property (nonatomic, retain) NSNumber * id_promocion;
@property (nonatomic, retain) NSString * descripcion;
@property (nonatomic, retain) NSString * titulo;
@property (nonatomic, retain) NSString * url;
@property (nonatomic, retain) NSString * fecha_inicio;
@property (nonatomic, retain) NSString * fecha_fin;
@property (nonatomic, retain) NSString * enlace;

@end
解决方案
最后出现的问题是,我有两个Coredata文件的引用(不知道为什么)。其中一个还在第一版上。我已经更改了该版本,一切正常。

我认为您的错误与轻量级迁移无关,因为您的应用程序不会在启动时崩溃(对于没有迁移的任何结构更改),但它会给您提供一个主要与模型类相关的运行时错误

我怀疑您的代码仍然使用旧生成的模型类或该旧类上的旧类别,但没有缺少该属性:(
#import“Promocion.h”
#import“Promocion+creation.h”

如果您的
coreData
&
managedObjects
-子类是在
Xcode 7
之前使用以前版本的
Xcode
创建的,则可能会出现这种情况。一个简单的检查方法是
cmd+点击任何
上的
#导入“promotion.h”
以查看内容

如果是这样的话,这里有一个解决问题的检查表(否则这可能仍然有助于其他面临类似问题的人):

-搜索和删除(或重命名!)
Promocion.h
:仅包含动态属性的旧生成文件(使用
Xcode7
版本之前的版本生成,请参见下面的详细信息)

-检查是否已在该类上创建了类别。
“Promocion+Xyz.h”
并将所有引用
#导入“Promocion+Xyz.h”
更改为
#导入“Promocion.h”

-重新创建
managedObject
子类

上下文

在Xcode7之前,每当我们创建
managedObject
子类时,.h包含动态属性,我们用来在该类上创建
category
,以添加方法,同时能够重新生成
managedObject
子类,而不会丢失我们创建的方法,因为它们在一个类别中

Xocde7
苹果回顾了这一点,现在动态属性出现在
ClassName+CoreDataProperties.h
中,以便于在主类上工作/添加方法,而无需添加类别,同时能够重新生成
managedObject
,因为只重新创建
ClassName+CoreDataProperties.h


因此,现在当我们重新创建以前用旧方法创建的
managedObject
子类时,我们需要确保引用#import“Promocion.h”正确的文件

看起来好像没有发生迁移,虽然类文件可能是正确的,但它们都依赖于底层的
NSManagedObject
来拥有该属性


您能否从已更新为Model 2的设备中获取sqlite文件,并检查sqlite文件的结构,看看新属性是否到位?如果不是,则表示迁移未启动,可能是定义为当前模型的错误版本。

尝试更改AppDelegate的datamodel函数中的行:

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"DataModel" withExtension:@"momd"];


最后出现的问题是,我有两个Coredata文件的引用(不知道为什么)。其中一个还在第一版上。我已更改了该版本,一切正常。

我会清理您的生成文件夹/删除派生数据,然后重试。@sschare派生数据在哪里?~/Library/Developer/Xcode/如果您在尝试使用此新属性时包含代码,会有所帮助。您能确保没有两份自动生成的文件副本吗
promotion
类?我用Xcode 6开始这个开发,但现在我用的是Xcode 7。然而,我尝试了“cmd+tap”,发现Promocion.h和Promocion+CoreDataProperties.h之间有一个循环引用。但我仍然不知道如何解决它。无论如何,我只有一个Promocion.h文件,所以我不明白如何才能弄乱引用。请展示您的[self-managedObjectModel]方法。如果您使用的是mergedModelFromBundles,请尝试使用nitWithContentsOfURL:NSString*path=[[NSBundle mainBundle]pathForResource:@“Model”of type:@“momd”];NSURL*url=[NSURL fileURLWithPath:path];NSManagedObjectModel*managedObjectModel=[[NSManagedObjectModel alloc]initWithContentsOfURL:url];我已经用AppDelegate中的代码片段更新了这个问题。您可以更新答案吗?您似乎已经在使用initWithContentsOfURL:url而不是mergedModelFromBundles…如何从iphone获取sqlite?将设备连接到运行Xcode的Mac,然后在“设备”窗口中,您可以下载应用包的内容。这个包的内部将是sqlite文件。我只在模拟器上测试。然后它将在您的应用程序支持目录中。看到这不起作用的答案,我收到了以下消息
CoreData:cannotloadnsmanagedobjectmodel。nil是非法的URL参数
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@interface Promocion : NSManagedObject

@property (nonatomic, retain) NSNumber * id_promocion;
@property (nonatomic, retain) NSString * descripcion;
@property (nonatomic, retain) NSString * titulo;
@property (nonatomic, retain) NSString * url;
@property (nonatomic, retain) NSString * fecha_inicio;
@property (nonatomic, retain) NSString * fecha_fin;
@property (nonatomic, retain) NSString * enlace;

@end
#pragma mark - Core Data stack

// Returns the managed object context for the application.
// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
- (NSManagedObjectContext *)managedObjectContext
{
    if (_managedObjectContext != nil) {
        return _managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        _managedObjectContext = [[NSManagedObjectContext alloc] init];
        [_managedObjectContext setPersistentStoreCoordinator:coordinator];
    }
    return _managedObjectContext;
}

// Returns the managed object model for the application.
// If the model doesn't already exist, it is created from the application's model.
- (NSManagedObjectModel *)managedObjectModel
{
    if (_managedObjectModel != nil) {
        return _managedObjectModel;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"DataModel" withExtension:@"momd"];
    _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
{
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }

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

    //Migration
    NSDictionary *options = @{
                              NSMigratePersistentStoresAutomaticallyOption  :@YES,
                              NSInferMappingModelAutomaticallyOption        :@YES
                              };

    NSError *error = nil;
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                                   configuration:nil
                                                             URL:storeURL
                                                        //options:nil
                                                         options:options//automatic migration
                                                           error:&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.

         Typical reasons for an error here include:
         * The persistent store is not accessible;
         * The schema for the persistent store is incompatible with current managed object model.
         Check the error message to determine what the actual problem was.


         If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.

         If you encounter schema incompatibility errors during development, you can reduce their frequency by:
         * Simply deleting the existing store:
         [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]

         * Performing automatic lightweight migration by passing the following dictionary as the options parameter:
         @{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}

         Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.

         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        //abort();
    }

    return _persistentStoreCoordinator;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"DataModel" withExtension:@"momd"];
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"DataModel 2" withExtension:@"mom"];