views:

72

answers:

2

I have a Class that I need to copy with the ability to make changes the value of a variable on both Classes. Simply put these classes need to remain clones of each other at all times. My understanding of the documentation is that I can do this using a shallow copy of the Class which has also been declared mutable. By shallow copying the pointer value for the variable will be cloned so that it is an exact match in both classes. So when I update the variable in the original the copy will be updated simultaneously. Is this right?

As you can see below I have used mutableCopyWithZone in the class I want to copy. I have tried both NSCopyObject and allocWithZone methods to get this to work. Although I'm able to copy the class and it appears as intended, when updating the variable it is not changing value in the copied Class.

- (id)mutableCopyWithZone:(NSZone *)zone {

//ReviewViewer *copy = NSCopyObject(self, 0, zone);
ReviewViewer *copy = [[[self class] allocWithZone:zone] init];
copy->infoTextViews = [infoTextViews copy];
return copy;
}

infoTextViews is a property declared as nonatomic, retain in the header file of the class being copied. I have also implemented the NSMutableCopying protocol accordingly.

Any help would be great.

+1  A: 

You are right, what you want is a shallow copy, but what you do is a deep copy. Change [infoTextViews copy] to [infoTextViews retain]

Small points.. allocWithZone? You mean allocWithZone:zone ? Plain old alloc would probably be fine.

Why mutableCopyWithZone: ? Are there mutable and immutable versions of ReviewViewer? You probably just want copyWithZone:

Note: If you override copyWithZone to perform a shallow copy you are specifying this behavoir everywhere the object is copied.

mustISignUp
oops, my bad. Yes I meant allocWithZone:zone I've amended the code above. Thanks.I've tried your suggestions but nothing has made a difference. Your right it probably doesn't need to be mutableCopy but having tried copy already I was willing to try everything. Perhaps the real problem is that I'm calling a method in the class that is copied to change the value of the variable. But this also doesn't make sense because the variable is a UITextView and the scroll doesn't work either on the class that has been copied.
Jim
So lets get this straight… you have tried it without sending the copy message to the infoTextViews object but you still end up with 2 infoTextViews objects?
mustISignUp
Yes, I've tried amending the code above without using copy or mutable attributes and used retain instead as per your suggestion. The class still appears twice. When I then call a method to change the value of the infoTextView on this class the value of this change does not effect the copied class's shared instance of this property.
Jim
Ok, i'd like to help here but it seems like something else is probably going on. The wording of your question hints at considerable confusion. The best that i can work out is that you want to copy an object 'a' so now you have 2 different objects in memory 'a1' and 'a2'. Both 'a1' and 'a2' share an instance variable 'b'. You say 'the real problem is that I'm calling a method in the class that is copied to change the value of the variable' - this makes no sense. You are not anywhere copying a class or calling a class method. You imply that infoTextViews is a UITextView. Is that the case?
mustISignUp
I think I understand now where the confusion is. I've misunderstood what copyWithZone is actually doing. It's not creating a new class it's only a creating an additional instance of the object. In short what I'm trying to do won't work the way I want it to. Yes, infoTextViews is a UITextView I was expecting the delegate of this view to perform in unison with the changes made on the original. hmm back to the drawing board. Sorry for the confusion. Thanks for your help.
Jim
A: 

Instead of two separate but identical objects, why can't you just use the same instance of your class in more than one place? That way there's only ever one object to modify. Once you make a copy (deep or shallow), you have two independent objects--changes to one don't propagate to the other.

Eric Skroch
I've tried this but for some reason the second instance of the same class doesn't draw the text that I'm trying to display.
Jim