我正在尝试进行轻量级迁移。我按照这个教程一步一步地制作了一个新的项目,并且一切都正常工作。但是现在我在另一个应用程序中,一切似乎都很顺利,但当我尝试访问新字段时,我会收到以下错误提示。
我已经选择了正确的模型。 这是模型Promocion.h。
以下是自动生成的文件: Promocion+CoreDataProperties.h
[Promocion setEnlace:]: unrecognized selector sent to instance 0x7fafe40bceb0
我已经选择了正确的模型。 这是模型Promocion.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;
@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.m
#import "Promocion.h"
@implementation Promocion
- (NSComparisonResult)compare:(Promocion *)otherObject {
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setDateFormat:@"MMMM dd,eeee HH:mm a z"];
NSDate *dateA = [dateformat dateFromString:self.fecha_inicio];
NSDate *dateB = [dateformat dateFromString:otherObject.fecha_inicio];
return [dateA compare:dateB];
}
@end
以下是自动生成的文件: 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
Promocion+CoreDataProperties.m #import "Promocion+CoreDataProperties.h"
Promotion+CoreDataProperties.m #import "Promotion+CoreDataProperties.h"
@implementation Promocion (CoreDataProperties)
@dynamic descripcion;
@dynamic fecha_fin;
@dynamic fecha_inicio;
@dynamic id_promocion;
@dynamic titulo;
@dynamic url;
@dynamic enlace;
@end
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”的字段?
NSString* className = NSStringFromClass([Promocion class]);
NSEntityDescription *entity = [NSEntityDescription entityForName:className inManagedObjectContext:context];
Promocion* obj = (Promocion*)[[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:context];
obj.id_promocion = [NSNumber numberWithInt:[[centro objectForKey:@"id_promo"] intValue]];
obj.titulo = [centro objectForKey:@"titulo"];
obj.descripcion = [centro objectForKey:@"descripcion"];
obj.url = [centro objectForKey:@"imagen"];
obj.fecha_inicio = [centro objectForKey:@"fecha_ini"];
obj.fecha_fin = [centro objectForKey:@"fecha_fin"];
obj.enlace = [centro objectForKey:@"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
另一次尝试
我重新创建了Promocion.h,删除了旧的,并使用Editor->Create NSOjecteManaged子类创建了一个新的。看起来它正在工作。但是当我使用git回滚到最后一个未更新core data提交并在设备上安装它,然后升级应用程序到具有新coredata模型的最后提交时,它开始再次失败。该死,我感到非常沮丧。
Partial Appdelegate.m
#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;
}
解决方案
最终出现问题的原因是我有两个Coredata文件的引用(不知道为什么)。其中一个仍然是第一个版本。我已经更改了那个版本,现在一切都按预期工作。