更新时间:2023-12-01 12:49:40
其属性和关系。或者,而是实体描述。
NSManagedObject, unlike NSObject, provides an API to iterate over its attributes and relationships. Or, rather, it's entity description does. It isn't a one-liner, though.
movement2 = [NSEntityDescription insertNewObjectForEntityForName:@"Movement"
inManagedObjectContext:self.managedObjectContext];
NSEntityDescription *entity = [movement entity];
for (NSString *propertyName in [entity propertiesByName]) {
[movement2 setValue:[movement valueForKey:propertyName] forKey:propertyName];
}
查看文档。
这将足以克隆大多数对象。如果数据库结构正确,那么以这种方式复制关系,它们的反向关系也将被更新。所以,如果你的运动与MovementDirection有一个关系,而MovementDirection有一个反向的一对多关系 parentMovements
,这个 parentMovements
set将在调用上述代码后同时具有
运动
和 movement2
。
This will be enough to clone most of the objects. If database structure is correct, then copying relationships this way, their inverse ones will be updated as well. So, if your Movement had a relationship with, say, MovementDirection, and MovementDirection has an inverse 1-to-many relation parentMovements
, this parentMovements
set will have both movement
and movement2
inside after you call the code above.