Re: copy versus arrayWithArray:
Re: copy versus arrayWithArray:
- Subject: Re: copy versus arrayWithArray:
- From: Chris Suter <email@hidden>
- Date: Fri, 18 Aug 2006 12:42:38 +1000
I'd have thought they'd be the same.
Why not iterate through the items using an index? Then you don't need
to make a copy.
e.g.
unsigned i = 0, count = [orig count];
id item;
while (i < count) {
item = [orig objectAtIndex:i];
// ...
if (shouldAddItem) {
[orig addObject:newObject];
}
// ...
if (shouldRemoveItem) {
[orig removeObjectAtIndex:i];
--count;
continue;
}
++i;
}
- Chris
On 18/08/2006, at 12:28 PM, Eric Scharff wrote:
I need to make a shallow copy of an array because I need to
iterate over the contents of a mutable array, and during that
iteration I will need to change the original array:
NSArray *temp = [NSArray arrayWithArray: orig];
NSEnumermerator *items = [temp objectEnumerator];
id item;
while ((item = [items nextObject]) {
// Possibly call [orig addObject: ] or removeObject:
}
Both NSMutableArray -copy and NSArray +arrayWithArray: methods
ought to solve the task for me. I lean toward the
+arrayWithArray: version because that way my temporary copy is
automatically autoreleased, which is precisely what I want.
However, which version is better/faster, especially if I have a
fairly large array?
Thanks,
-Eric
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
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