• 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: NSArray retaining and releasing
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSArray retaining and releasing


  • Subject: Re: NSArray retaining and releasing
  • From: Graham Cox <email@hidden>
  • Date: Sat, 3 May 2008 16:14:02 +1000


On 3 May 2008, at 4:00 pm, Hrishikesh Muruk wrote:


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?

No, they are retained by the array, and are released when the array is released.


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)?

No they don't - you have to implement copy if you claim to support the NSCopying protocol for your own objects - but most Cocoa objects themselves do implement it.


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


You can use a category - I use the following, which allows you to do something like arrayCopy = [oldArray deepCopy]; However it's not magic - it just loops through and copies each object just as you do above - the advantage is that it deals with nested arrays and dictionaries. The disadvantage is that it *always* does this, for every object, which might be a blunt instrument in some cases. In general, you want to avoid the need to deep copy things.

(note deepCopy is implemented to follow copy semantics - caller is to release the returned object).


@implementation NSDictionary (DeepCopy)


- (NSDictionary*) deepCopy { NSMutableDictionary* copy; NSEnumerator* iter = [self keyEnumerator]; id key, cobj; copy = [[NSMutableDictionary alloc] init]; while(( key = [iter nextObject])) { cobj = [[self objectForKey:key] deepCopy]; [copy setObject:cobj forKey:key]; [cobj release]; }

	return copy;
}


@end


#pragma mark - @implementation NSArray (DeepCopy)

- (NSArray*)			deepCopy
{
	NSMutableArray*			copy;
	NSEnumerator*			iter = [self objectEnumerator];
	id						obj, cobj;

	copy = [[NSMutableArray alloc] init];

	while(( obj = [iter nextObject]))
	{
		cobj = [obj deepCopy];
		[copy addObject:cobj];
		[cobj release];
	}

	return copy;
}


@end


#pragma mark - @implementation NSObject (DeepCopy)

- (id)					deepCopy
{
	return [self copy];
}


@end

_______________________________________________

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


References: 
 >NSArray retaining and releasing (From: Hrishikesh Muruk <email@hidden>)

  • Prev by Date: Saving/restoring PDFAnnotationTextWidget stringValues with PDFKit 10.5
  • Next by Date: Re: NSArray retaining and releasing
  • Previous by thread: NSArray retaining and releasing
  • Next by thread: Re: NSArray retaining and releasing
  • Index(es):
    • Date
    • Thread