Create a NSSet from NSArray based on property

前端 未结 5 1848
礼貌的吻别
礼貌的吻别 2021-01-12 04:22

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 pro

5条回答
  •  旧时难觅i
    2021-01-12 05:03

    I have created a simple library, called Linq to ObjectiveC, which is a collection of methods that makes this kind of problem much easier to solve. In your case you need the Linq-to-ObjectiveC distinct method:

    NSSet* dictionary = [NSSet setWithArray:[sourceArray distinct:^id(id item) {
        return [item type] ;
    }]];
    

    This returns a set where each item has a distinct type property.

提交回复
热议问题