I have a class A that contains a property of class B. Class B has a weak reference to its "parent" class A. Both classes implement NSCopying.
I don't know how exactly NSCopying should be implemented in class B. I see the two obvious choices:
- assign the parent property to the copied class
- copy the parent property and assign it to the copied class
In the first case, the parent property in B will point the original A. In the second case the parent property is an entirely new copy.
How do I copy class B correctly so that the parent property will points to the newly created copy of A during the process of NSCopying?
-(id) copyWithZone:(NSZone*)zone
{
MyClassB* copy = [[[self class] allocWithZone:zone] init];
copy->_parent = _parent; // <<-- should reference the new copy of A, but how?
return copy;
}
I suppose that the easiest approach would be to update the parent property from the copyWithZone: in class A. But is there any way I can do so from within class B?