我有两个实体:
- 票
- 票务已解决
两个实体具有相同的属性。将 a 复制Ticket
NSManagedObject
到的最有效方法是TicketResolved
NSManagedObject
什么?
我正在考虑使用 Category:Ticket+Copy
是最便宜的方式吗?如果是这样,我将不得不#import
同时在类别文件中。 Ticket
TicketResolved
这是我想出的,有人可以建议这是否是正确的方法。我正在NSManagedObjectSubclass
为每个实体使用。
方法Ticket+Copy
:
-(TicketResolved *)copyObjects:(Ticket *)ticket
{
TicketResolved *ticketResolved = [NSEntityDescription insertNewObjectForEntityForName:@"TicketResolved" inManagedObjectContext:self.managedObjectContext];
ticketResolved.attribute = ticket.attribute;
// Assign rest of the ticketResolved attributes values this way
return ticketResolved;
}
现在调用方法
#import Ticket;
#import Ticket+Copy;
@implementation
....
Ticket *ticket = [NSEntityDescription insertNewObjectForEntityForName:@"Ticket" inManagedObjectContext:self.managedObjectContext];
TicketResolved *newTicketResolved = [ticket copyObjects:ticket];
// 'newTicketResolved' now has all the keys/values that 'ticket' had.
这是一种正确的方法还是有更简单的方法来做到这一点?