如何正确实现子类化NSManagedObject的自定义初始值设定项



我想知道创建NSManagedObject子类的类的初始值设定项的正确方法是什么。

目前我正在这样初始化:

-(id)initWithXML:(TBXMLElement *)videoXML
{
    // Setup the environment for dealing with Core Data and managed objects
    HenryHubAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSEntityDescription *entityHubPieceVideo = [NSEntityDescription entityForName:@"HubPieceVideo" 
                                                           inManagedObjectContext:context];
    self = [[HubPieceVideo alloc] initWithEntity:entityHubPieceVideo insertIntoManagedObjectContext:context];
    // do stuff and then save
    NSError *error;
    if(![context save:&error]) 
    {
        NSLog(@"HubPiece video save context error: %@ %@", error, [error userInfo]); 
    }
}

似乎有些人也这样做。

刚刚发现NSManagedObject引用说:

如果实例化托管对象您必须直接致电指定的初始化器(initWithEntity:insertIntoManagedObjectContext:)。

最新更新