Convert NSArray to NSDictionary

后端 未结 6 1987
清酒与你
清酒与你 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条回答
  •  温柔的废话
    2020-12-02 23:04

    - (NSDictionary *) indexKeyedDictionaryFromArray:(NSArray *)array
    {
        NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] init];
        [array enumerateObjectsUsingBlock:
         ^(id obj, NSUInteger idx, BOOL *stop){
             NSNumber *index = [NSNumber numberWithInteger:idx];
             [mutableDictionary setObject:obj forKey:index];
         }];
        NSDictionary *result = [NSDictionary.alloc initWithDictionary:mutableDictionary];
        return result;
    }
    

提交回复
热议问题