NSArray retaining and releasing
NSArray retaining and releasing
- Subject: NSArray retaining and releasing
- From: Hrishikesh Muruk <email@hidden>
- Date: Sat, 3 May 2008 11:30:26 +0530
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 (email@hidden)
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:
This email sent to email@hidden