2012-07-09 36 views
1

我想使用核心数据来坚持一些实体,如事件。核心数据 - initWithEntity导致无法识别的选择器发送到实例

所以我使用的类DSManagedObjectEvent

DSManagedObject扩展NSManagedObject并具有所有的实体可以使用一般方法。 Event延伸DSManagedObject

以下代码是DSManagedObject.h.m.m中的相关代码只是getContext-方法。

@interface DSManagedObject : NSManagedObject 

+ (NSManagedObjectContext *)getContext; 
- (NSArray*)getEntitiesForName:(NSString*)_entityName context:(NSManagedObjectContext*)_context; 
- (Event*)getEntityForName:(NSString*)_entityName forEventId:(NSInteger)_eventId context:(NSManagedObjectContext*)_context; 
- (bool)deleteEntityForName:(NSString*)_entityName forEventId:(NSInteger)_eventId context:(NSManagedObjectContext*)_context; 

@end 


@implementation DSManagedObject 

+ (NSManagedObjectContext *)getContext { 

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


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; 
    NSURL *storeUrl = [NSURL fileURLWithPath:[basePath stringByAppendingFormat:@"DesertStorm.sqlite"]]; 
    NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[NSManagedObjectModel mergedModelFromBundles:nil]]; 
    NSError *error = nil; 

    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) { 
     NSLog(@"error loading persistent store.."); 
     [[NSFileManager defaultManager] removeItemAtPath:storeUrl.path error:nil]; 
     if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) { 
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
      abort(); 
     } 
    } 



    NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init]; 
    [context setPersistentStoreCoordinator:persistentStoreCoordinator]; 

    return context; 


} 

现在在班上Event我想打电话给initWithEntity但随后的错误[Event managedObjectModel] unrecognized selector sent to instance发生。 是什么原因? :(

@interface Event : DSManagedObject 

@property (assign)    NSInteger eventId; 

@end 


@implementation Event 

@dynamic eventId; 

- (id)init { 

    NSEntityDescription *entity = [NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:[DSManagedObject getContext]]; 
    self = [self initWithEntity:entity insertIntoManagedObjectContext:[DSManagedObject getContext]]; // error occurs 
    self = [super init]; 
    if (self) { 

    } 
    return self; 
} 

... 

@end 

我使用的核心数据,因此表现出理解是新) 感谢您的帮助

PS:如果你想知道为什么我重写init - 方法......复杂的原因^^

回答

0

我解决了这个问题,在init-方法。 我还是覆盖的方法(我知道我不应该,但是...无论如何)

下面的代码

- (id)init { 
    return [NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:[DSManagedObject getContext]]; 
} 

这将返回一个NSManagedObject,并没有出现错误:)

5

Core Data DOC:

In a typical Cocoa class, you usually override the designated initializer (often the init method). In a subclass of NSManagedObject, there are three different ways you can customize initialization —by overriding initWithEntity:insertIntoManagedObjectContext:, awakeFromInsert, or awakeFromFetch. You should not override init. You are discouraged from overriding initWithEntity:insertIntoManagedObjectContext: as state changes made in this method may not be properly integrated with undo and redo. The two other methods, awakeFromInsert and awakeFromFetch, allow you to differentiate between two different situations:

所以soultions是覆盖initWithEntity:insertIntoManagedObjectContext:○ r利用awakeFromInsertawakeFromFecth。如果你想,ovveride前,因为你调用或insertNewObjectForEntityForName:inManagedObjectContext:后调用 。

是否有你想实现的特定目标?

编辑

尝试覆盖的initWithEntity:insertIntoManagedObjectContext:代替init

- (id)initWithEntity:(NSEntityDescription*)entity insertIntoManagedObjectContext:(NSManagedObjectContext*)context  
{  
    self = [super initWithEntity:entity insertIntoManagedObjectContext:context]; 
    if (self != nil) { 

     // Perform additional initialization. 

    } 

    return self;  
} 

的方法是NSManagedObject指定初始化。您不能简单地通过发送init来初始化管理对象。请参阅NSManagedObject class for further info。

+0

它带我到目标;)你知道为什么错误'CoreData:error:无法调用NSManagedObject类'Event'上的指定初始值设定项''时,我不会调用'initWithEntity'?是否必须调用'initWithEntity'? – 2012-07-09 11:21:46

+0

@TobiWeißhaar我添加了一个编辑... – 2012-07-09 12:05:11

+0

感谢您的帮助。它帮助我更多地了解核心数据。我解决了这个问题(查看我的回答) – 2012-07-09 12:49:31

相关问题