Search code examples
objective-cnsarraynsnumbernsset

ObjC: find if number is present in collection


I have a list of long values:

300210, 300211, 310210, 310211, ...

I'm looking for the best way to check whether a number is present in a collection. The collection is non mutable and this check can possibly happen hundreds of time per second (it's part of a physics engine collision presolving).

If using an NSArray, I'm to use NSNumbers. These are objects - Is the containsObject: method using hashcodes? Or does it consistently use a value comparison (rather than pointer address)?

How about NSSet? I know it has a member: method to use isEqual: but no practical experience with it.

thanks for your help find the best way to address this.


Solution

  • I would suggest turn on objective-C++ and use std::set. It's much faster then NSSet. You will need:

    in header:

    #include <set>
    using namespace std;
    

    in code:

    set<int> numberCollection;