0

I got a NSMutableArray which I want to add strings to. I need to check if that string already exists on the array before adding ths string to it. Does anyway know a good way to do it?

Thanks!

3 Answers 3

8

If order doesn't matter, the simplest way would be to switch to using an NSMutableSet. Sets are unordered and NSSets and NSMutableSets can contain each object only once. Only a counted set (NSCountedSet) can contain the same object multiple times.

If order does matter, continue using NSMutableArray, and use -containsObject: to check that the object is not already there before adding it.


Edit: And as of Lion, we now have NSOrderedSet and NSMutableOrderedSet! Chocolate in my peanut butter! Peanut butter in my chocolate!

Like an unordered set, you get fast membership testing, the prohibition of duplicates, and, of course, set operations (union, intersection, etc.). Like an array, you get ordered access, including both reliable-order enumeration and access by index.

So, if you need to reject duplicates but at the same time preserve order, Lion's NS{,Mutable}OrderedSet classes are for you.

Sign up to request clarification or add additional context in comments.

Comments

1

If you just want to do what you stated once, just use containsObject:, as in

if (![myArray containsObject:theObject]) [myArray addObject:theObject];

Note that this does a linear search through the array, and thus isn't appropriate if you're going to be using this operation a lot. If you are, and you don't need ordering, you should use NSSet. If you do need ordering, you could use both an NSArray and NSSet that are kept in sync (e.g. always add/delete the same object from both collections at the same time).

6 Comments

Doesn't containsObject check the reference pointer? If you had an outside string and wanted to compare the value I don't know if this would work as intended.
jocull: The documentation says membership testing is performed by comparing the objects' hash values and then asking them whether they are equal (isEqual:). developer.apple.com/mac/library/documentation/Cocoa/Reference/…
If it doesn't work, then the object doesn't think it's equal to anything in the array. So either your object really isn't equal to something in the array, or it has a bad -hash or -isEqual: method.
-containsObject: definitely doesn't check reference pointers; that's -indexOfObjectIdenticalTo:'s job
@jocull: You're looking at the documentation for NSSet. And I agree, that's very vague. The documentation for -[NSArray conainsObject:] is a lot more specific and explicitly states it relies on -hash and -isEqual:. If you're using NSSet, you can just use -member: instead of -containsObject: if you want to be certain it's doing equality testing.
|
0

I preferred the NSPredicate which describe in here

In short

NSMutableArray *array = [NSMutableArray arrayWithObjects:@"Nick", @"Ben", @"Adam", @"Melissa", nil];

NSPredicate *bPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES[c] %@", your_search_key];
NSArray *beginWithB = [array filteredArrayUsingPredicate:bPredicate];

if ([beginWithB count] > 0)
{
    //Handle duplicate
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.