IPhone NSArray from Dictionary of Dictionary Values

I have a dictionary of dictionaries that is returned to me in JSON format

{
    "neverstart": {
        "color": 0, 
        "count": 0, 
        "uid": 32387, 
        "id": 73129, 
        "name": "neverstart"
    }, 
    "dev": {
        "color": 0, 
        "count": 1, 
        "uid": 32387, 
        "id": 72778, 
        "name": "dev"
    }, 
    "iphone": {
        "color": 0, 
        "count": 1, 
        "uid": 32387, 
        "id": 72777, 
        "name": "iphone"
    }
}

I also have an NSArray containing the identifier needed for the element. for example [72777, 73129]

What I need is to get the id => name dictionary for the elements in the array. I know this is possible by iterating through an array and then iterating over all the values ​​in the Dictionary and checking the values, but it seems like there should be a shorter method for this.

Excuse my ignorance as I am just trying to find my way around the iPhone SDK and learn Objective-C and Cocoa.

+3
source share
3 answers

-, JSON, , BSJSONAdditions / json-framework, - JSON Cocoa . , , NSDictionary JSON.

. , , - Cocoa , , . ... ( iPhone, , .)

NSDictionary* jsonDictionary = ...
NSDictionary* innerDictionary;
NSArray* requiredIDs = ...
NSMutableDictionary* matches = [NSMutableDictionary dictionary];
for (id key in jsonDictionary) {
    innerDictionary = [jsonDictionary objectForKey:key];
    if ([requiredIDs containsObject:[innerDictionary objectForKey:@"id"]])
        [matches setObject:[innerDictionary objectForKey:@"name"]
                    forKey:[innerDictionary objectForKey:@"id"]];
}

, . , [NSMutableDictionary dictionary] .

+5

NSDictionary:

+ (id)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys
+2

Source: https://habr.com/ru/post/1711333/


All Articles