所以我在Objective-C中遇到了这个问题。让我设定问题的阶段。
我有一个约束,它不允许我修改对象的属性。因为它将声明我正在修改该对象而不将其置于写入和提交事务之间
ObjectA* constrainedObject = ...; // Retrieved from source with constraint applied.
constrainedObject.name = @"John"; // Assert tripped
[db writeTransaction];
constrainedObject.name = @"John"; // Does not assert
[db commitTransaction];
但是,如果我创建一个新对象,我可以解决此限制并通过分配对unconstrainedObject
的引用来修改对象。
ObjectA* constrainedObject = ...; // Retrieved from source with constraint applied.
ObjectA* unconstrainedObject = [[ObjectA alloc] init];
unconstrainedObject.id = contrainedObject.id;
// Change name here
unconstrainedObject.name = @"John";
unconstrainedObject.home = @"Mansion";
unconstrainedObject.age = constrainedObject.age; // Keep old property
// This illustrates the problem.
// I still want to keep some of the properties of the old
// object but have to manually type it in.
所以我的问题是如何从constrainedObject
检索所有属性引用而不手动输入全部内容?
有没有办法检查NSObject
的键值属性并将其映射到同一类型的另一个NSObject
?