If I make an NSArray in the following manner:

NSArray *newArray = [NSArray arrayWithArray:oldArray];
[newArray retain];

Now I it is my responsobolity to send a release message to newArray. But am I responsible to send release messages to the contents of newArray?

If the oldArray contained pointers to objects then newArray will also contain pointers pointing to the same objects as oldArray. Do I have to send a retain message to each of the entries in newArray to ensure they stay around? (of course if I send those retain message I am responsible to release them later)

How do I do a deep copy? If oldArray is an NSArray holding pointers of type MyObj*. If I want a newArray that duplicates the contents of oldArray. By that I mean I want the pointers in newArray to point to new objects of type MyObj rather than the same objects as oldArray how do I do this?

Would this work:

for(i=0;i<[oldArray count]; i++) {

   MyObj *temp = [[oldArray objectAtIndex:i] copy];
   [newArray addObject:temp];
}

But this requires MyObj to support the copy message. Do all objects in cocoa automatically support copy message (if they inherit from NSObject)?

Is there an easier way to do deep copy of arrays?

Hrishi

---
www.greenwaysroad.com


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to