I am adding objects (NSNumbers in this case) to NSMutableArray, and I wanted to check how best to check for duplicates in an array before adding. (I.e.)
Number to add
if (NSMutableArray does not contain Number) {
add Number
}
EDIT:
Thank you so much, I was lucky in NSArray today, but I completely missed "containsObject". That would be very good, but looking at the NSMutableSet is a lot more than what I was looking for. One last question if I can:
while([mySet count] < 5) {
NSNumber *numberToAdd = [NSNumber numberWithInt:random() %10];
[mySet addObject:numberToAdd];
}
I don’t think it really matters, but is it better to check if the "containsObject" set is set or just throw away the duplicate and continue.
while([mySet count] < 5) {
NSNumber *numberToAdd = [NSNumber numberWithInt:random() %10];
if(!mySet containsObject:numberToAdd) [mySet addObject:numberToAdd];
}
Again much appreciated, it is really great and will save me a ton of time.
Gary
source
share