I have an NSArray formed with objects of a custom class. The class has 3 (city, state, zip) string properties. I would like to get all unique state values from the array.
I did read through the NSPredicate class but couldn’t make much of how to use it in this case. The only examples I could find were for string operations.
Can someone please help me out?
The totally simple one liner:
The trick is the
valueForKey:method ofNSArray. That will iterate through your array (myArrayOfCustomObjects), call the-statemethod on each object, and build an array of the results. We then create anNSSetwith the resulting array of states to remove duplicates.Starting with iOS 5 and OS X 10.7, there’s a new class that can do this as well:
NSOrderedSet. The advantage of an ordered set is that it will remove any duplicates, but also maintain relative order.