Re: copy versus arrayWithArray:
Re: copy versus arrayWithArray:
- Subject: Re: copy versus arrayWithArray:
- From: Fredrik Olsson <email@hidden>
- Date: Fri, 18 Aug 2006 16:33:56 +0200
Greg Hurrell skrev:
El 18/08/2006, a las 15:32, Nir Soffer escribió:
On Aug 18, 2006, at 5:28 AM, Eric Scharff wrote:
NSArray *temp = [NSArray arrayWithArray: orig];
I would choose: [[orig copy] autorelease] because it is shorter and
makes your intention more clear.
Except that the two statements aren't equivalent. A copy of a mutable
array yields another mutable array (the poster said that "orig" is a
mutable array). Conversely, NSArray's arrayWithArray: will return an
immutable array (which is arguably better in this case because in many
cases immutable arrays are faster than mutable ones).
To me the intention is clearer in the first statement (ie. that the
caller wants an immutable, autorelease copy of the original array).
The intention of the second statement seems confused (ie. like the
caller wanted an immutable copy but ended up mistakenly getting a
mutable one).
I might have misinterpreted the docs, but I am very confident that copy
will return an immutable copy even on a NSMutableArray.
If you read the docs for the NSCopying protocol is clearly says: "The
copy returned is immutable if the consideration “immutable vs. mutable”
applies to the receiving object". While the NSMutableCopying protocol
says that mutableCopy "The copy returned is mutable whether the original
is mutable or not".
I have always used this idiom to use copy on NSMutableFoos to return
NSFoos, and mutableCopy on NSFoos to get NSMutableFoos.
// Fredrik Olsson
Cheers,
G
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden