iphone - 如何为子类化的 NSManagedObject 正确实现自定义初始化程序

标签 iphone objective-c ios core-data 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]); 
    }
}

好像some其他 also这样做。

最佳答案

刚发现NSManagedObject reference说:

If you instantiate a managed object directly, you must call the designated initializer (initWithEntity:insertIntoManagedObjectContext:).

关于iphone - 如何为子类化的 NSManagedObject 正确实现自定义初始化程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6196870/

相关文章:

iphone - iOS UIScrollView clipToBounds = 没有检测到 subview

objective-c - CALayer : Single pixel line looks like 2 pixels

objective-c - 尝试实现 MIDIReceived 但我不知道结果发送到哪里

jquery - 链接图片在 iOS 6 中不可点击,但文本可点击

ios - 如何在 iOS 版 Firebase Invites 中自定义邀请对话框表

ios - 为什么 UITableViewDataSource 或 UITableViewDelegate 不需要在 UITableView 中定义为弱?

iphone - iOS 5 JSON 分级别解析

ios - for语句中的这行代码是什么意思或做什么?

iPhone架构

iphone - PerformSelector 和匹配的cancelPreviousPerformRequestWithTarget