0

So I have this problem in Objective-C. Let me set the stage of the problem.

I have a constraint, that doesn't allow me to modify the properties of an object. As it will assert that I am modifying that object without putting it between a write and commit transaction

ObjectA* constrainedObject = ...; // Retrieved from source with constraint applied.
constrainedObject.name = @"John"; // Assert tripped
[db writeTransaction];
constrainedObject.name = @"John"; // Does not assert 
[db commitTransaction];

However if I create a new object, I can work around this limitation and modify the object by assigning the references to the 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.

So my question is how do retrieve all the property references from the constrainedObject without manually typing that all out?

Is there a way to inspect and map the key value properties of an NSObject over to another NSObject of the same type ?

Related: https://github.com/realm/realm-cocoa/issues/1992

MattP
  • 1,920
  • 1
  • 15
  • 22
George Host
  • 980
  • 1
  • 12
  • 26
  • I'm having a hard time understanding. Could you not simply do `ObjectA *unconstrainedObject = constrainedObject.copy;`? Are the constraints preventing that? – benhameen May 26 '15 at 16:49
  • does copy references? to the properties of the object or direct copies and point to new objects in memory ? That is does copy do this ObjectA a ->property -> A ObjectA b ->property -> A // Same reference to A in memory ObjectA a ->property A ObjectA b ->property B // Copy of A not a reference to A Shallow or Deep ? – George Host May 26 '15 at 17:59
  • Whether `copy` creates a deep or shallow copy is dictated by the `ObjectA` implementation of `-copyWithZone:` (it's hard to say without seeing the class, but I'm not sure it matters in your case). This is also where the manual property mapping is done, which still requires typing everything out, but is at least done in one place. – stefandouganhyde May 28 '15 at 09:44

0 Answers0