How to test if an NSArray or NSSet contains an object

There’s a handy method in the NSArray class that lets us check if an array contains a particular object or not. The containsObject method returns a BOOL for us to evaluate.

Consider this:

Likewise, we can check if our array does not contain a particular object by testing the opposite:

Both the NSMutableArray and NSSet classes has the same method, so we can check for the (un-)presence of our objects in mutable arrays and sets the same way.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.