• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: help needed with OO concepts when copying
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: help needed with OO concepts when copying


  • Subject: Re: help needed with OO concepts when copying
  • From: Chris Giordano <email@hidden>
  • Date: Wed, 12 Feb 2003 09:10:55 -0500

Denis,

On Tuesday, February 11, 2003, at 06:25 PM, Denis Stanton wrote:

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.

No problem. Glad to help. I was feeling less than productive work-wise, and missing my days as an academic to some degree, so given that I actually had something potentially useful to say, I figured I'd offer what I could.

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?

You could do that. As Seth "The Amazing Llama" Roby wrote, you could also see if [blocks deepCopy] would work. To some degree, whatever works, but rolling your own copyWithZone: method does have the advantage that you get to control how your items are copied (so, for example, you can make sure that any items within each Block element are copied, rather than having the copy point to the same item, giving you the same issue you were having with the array). Bottom line is get it working. Chances are there are a few ways of getting there. Once it works, you can see if there is a better way (maybe one is faster, etc.).

chris
_______________________________________________
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.

References: 
 >Re: help needed with OO concepts when copying (From: Denis Stanton <email@hidden>)

  • Prev by Date: Overlapping sibling level sub-views ?
  • Next by Date: Re: WSMakeStubs code issues
  • Previous by thread: Re: help needed with OO concepts when copying
  • Next by thread: RE: help needed with OO concepts when copying
  • Index(es):
    • Date
    • Thread