Create a NSSet from NSArray based on property

some_id picture some_id · Mar 7, 2013 · Viewed 10.4k times · Source

How does one create a NSSet of objects from an array based on a property.

e.g. Array of objects, each with a strong reference to a type property, and multiple occurrences of each type exist in the array. How can this be turned into an NSSet holding a single object of each type.

Answer

Kirby Todd picture Kirby Todd · Mar 7, 2013
NSSet *distinctSet = [NSSet setWithArray:[array valueForKeyPath:@"@distinctUnionOfObjects.property"]];