Re: Deleting duplicates in NSMutableArray
Re: Deleting duplicates in NSMutableArray
- Subject: Re: Deleting duplicates in NSMutableArray
- From: Darkshadow <email@hidden>
- Date: Mon, 5 Jul 2004 15:54:49 -0400
On Jul 5, 2004, at 12:20 AM, Johan Kool wrote:
Grmbl.... as always, that one clear moment comes right after pressing
that "Send" button...
For the archives, this should do it:
// Delete duplicates
for (i=0; i < [mutArray count]; i++ ) {
for (j=(i+1); j < [mutArray count]; j++) {
if ([[mutArray objectAtIndex:i] isEqualTo:[mutArray
objectAtIndex:j]]) {
[mutArray removeObjectAtIndex:j];
j--; // everything shifts down, so at j there is now an
entry we haven't yet checked
}
}
}
NS[Mutable]Array also has a method -containsObject: that you may be
able to use:
-----------------------------------------
containsObject:
- (BOOL)containsObject:(id)anObject
Returns YES if anObject is present in the array. This method determines
whether an object is present in the array by sending an isEqual:
message to each of the array's objects (and passing anObject as the
parameter to each isEqual: message).
-----------------------------------------
If you don't want duplicate objects at all, you could use that when
you're adding objects to the array so that it wouldn't add a duplicate
object.
Darkshadow (sometimes known as Mike Nickerson)
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.