我想知道创建自己的类初始化器的正确方法是什么,该类是 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 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/