Re: help needed with OO concepts when copying
Re: help needed with OO concepts when copying
- Subject: Re: help needed with OO concepts when copying
- From: Denis Stanton <email@hidden>
- Date: Wed, 12 Feb 2003 12:25:00 +1300
Hi Chris
Thank you for your generous support in explaining the differences
between pointers and data. I had some idea of this but I had failed to
grasp that even though I was copying the array, not taking a pointer to
it, the array itself consisted of pointers.
On Wednesday, February 12, 2003, at 11:05 AM, Chris Giordano wrote:
>
Denis,
>
>
Actually, you are making a copy of the array (or two when you copy the
>
array you send with the setBlock: message) . Unfortunately, you're
>
not also copying the elements of the array, so all two (or three)
>
arrays contain the same set of objects. That may not make things
>
clearer. Let me try to elaborate.
I see now that although my copied array is a different object from the
original in that I can add new objects to the copy without seeing them
in the original, the copied array consists of pointers to the same set
of real data items. My tests all consisted of editing the data
content, and as the copied array contained identical pointers the same
content was showing on both arrays.
>
So, in your setBlocks: method (which has a few other issues -- you're
>
allocating myBlocks, and then leaking that one when you use "myBlocks
>
= [newBlocks copy]"),
There was some left-over code there from one of my many attempts at
working around the problem.
I tried to cut it down to make the example more straight forward, but I
left :
NSMutableArray *myBlocks = [[NSMutableArray alloc] init];
myBlocks = [newBlocks copy];
when I should have written
NSMutableArray *myBlocks = [newBlocks copy];
>
you'd want to do something like the following.
>
>
- (void)setBlocks: (NSMutableArray *) newBlocks
>
{
>
if (blocks == newBlocks) return; // save yourself some work in case
>
they are the same
>
>
NSMutableArray * oldBlocks = blocks;
>
blocks = [[NSMutableArray alloc] initWithArray:newBlocks
>
copyItems:YES];
>
[oldBlocks release];
>
}
That looks a whole lot better.
>
After this method is performed, blocks will point to a new array with
>
objects that are copies of those in the original array. Assuming that
>
your blocks are such that they will produce a different object when
>
sent a "copyWithZone:" message, you should have what you need.
The code produced the following error:
2003-02-12 12:11:06.341 Contact[17673] *** -[Block copyWithZone:]:
selector not recognized
2003-02-12 12:11:06.341 Contact[17673] An uncaught exception was raised
2003-02-12 12:11:06.342 Contact[17673] *** -[Block copyWithZone:]:
selector not recognized
2003-02-12 12:11:06.342 Contact[17673] *** Uncaught exception:
<NSInvalidArgumentException> *** -[Block copyWithZone:]: selector not
recognized
Contact has exited due to signal 5 (SIGTRAP).
Does this mean I need to write my own copyWithZone method for my Block
class?
Denis
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.