Convert NSArray to NSDictionary

后端 未结 6 1984
清酒与你
清酒与你 2020-12-02 22:13

How can I convert an NSArray to an NSDictionary, using an int field of the array\'s objects as key for the NSDictionary?

6条回答
  •  -上瘾入骨i
    2020-12-02 23:08

    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 toDictionary method, where your 'int' field is specified via a key selector:

    NSDictionary* dictionary = [sourceArray toDictionaryWithKeySelector:^id(id item) {
        return [item intField];
    }];
    

    This returns a dictionary where the keys are given by the intField in the source array.

提交回复
热议问题