On Fri, 29 Aug 2008 12:13:34 -0400, "Michael Ash" <[EMAIL PROTECTED]> wrote:

On Thu, Aug 28, 2008 at 9:50 PM, Gerriet M. Denkmann
<[EMAIL PROTECTED]> wrote:
While if fully agree with you about valid assumptions and so, I am still wondering what is the disadvantage of forgetting about NSEnumerator, Fast
Enumeration and the like and simply doing:

unsigned count = [ array count ];
if ( count == 0 ) return;
for( unsigned i = count - 1;; i--)
{
       id a = [ array objectAtIndex: i ];
       if ( a is not nice ) [ array removeObjectAtIndex: i ];
       if ( i == 0 ) break;
}

Maybe not "Fast" as in "Fast Enumeration" but maybe simpler and faster than
copying all the "nice" objects into a secondary array.

The disadvantage is simply that it's more code (and therefore more
potential for bugs) and it may be slower.

If performance were not a concern then I'd much prefer something like this:

for(id obj in [NSArray arrayWithArray: array])
    if( condition(obj) )
        [array removeObject:obj];

And if performance is a concern, I'd probably prefer something like this:

NSMutableIndexSet *indexes = [NSMutableIndexSet indexSet];
unsigned index = 0;
for( id obj in array )
{
    if(condition(obj))
        [indexes addIndex:index];
    index++;
}
[array removeObjectsAtIndexes:indexes];

But this is really getting into the realm of personal preference.
Certainly I can't tell you whether this is faster than yours or not,
and the amount of code is such that I wouldn't say that it's
absolutely obvious that it's prettier.

And lastly I just want to note that if your condition fits easily into
an NSPredicate, you can do this whole business as a simple one-liner.

You are right. removeObjectsAtIndexes: is much better than my suggestion. I did not know about this method.
Another thing I should check out is NSPredicate.

Thanks for enlightening me!


Kind regards,

Gerriet.

_______________________________________________

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