0
我有一个NSManagedObjectModel,我可以在运行时生成,但不能在运行时使用它,因为我需要实体的类文件。如何在运行时从NSManagedObjectModel生成核心数据访问器类文件(.m/.h文件)?
如何生成这些类,并可能将它们粘贴到NSBundle中以在运行时加载它们?
谢谢!
我有一个NSManagedObjectModel,我可以在运行时生成,但不能在运行时使用它,因为我需要实体的类文件。如何在运行时从NSManagedObjectModel生成核心数据访问器类文件(.m/.h文件)?
如何生成这些类,并可能将它们粘贴到NSBundle中以在运行时加载它们?
谢谢!
最好的解决方案是添加协议。
您不需要托管对象类的实现。
@protocol NSManagedObjectProtocol <NSObject>
//Add NSManagedObject methods here. Like:
- (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:(NSManagedObjectContext *)context
@end
@protocol Person <NSManagedObjectProtocol>
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSDate *birthDate;
@end
使用该协议访问您的对象。
此代码会存在某种经理:
NSManagedObjectContext *context = //Get the context.
NSError *error = nil;
id<Person> p = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context];
[p setName:@"PersonName"];
if ([context save:&error]) {
//Handle error
}
想这是有道理的。我的应用程序的其余部分是使用set____:这样的选择器来实现的!谢谢 :) –