2011-11-05 46 views

回答

0

最好的解决方案是添加协议。
您不需要托管对象类的实现。

@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 
} 
+0

想这是有道理的。我的应用程序的其余部分是使用set____:这样的选择器来实现的!谢谢 :) –

相关问题