Parsing a Dictionary of Arrays from a Plist file

87 Views Asked by At

I am trying to work with a specific data layout. We use a plist file which is setup in the following way: Root (Dictionary):
- 1.1 (Array)
-- Item 0 (Dictionary)
--- Title
--- Icon
-- Item 1 (Dictionary)
--- Title
--- Icon
- 1.2 (Array)
-- Item 0 (Dictionary)
--- Title
--- Icon

This information from the plist is parsed into a Dictionary which logs out as follows:

{
    "1.0" =     (
                {
            detail = "Detail 0";
            icon = "Pull to Refresh";
            title = "Item 0";
        },
                {
            detail = "More easily refresh a subscription or playlist.";
            icon = "Pull to Refresh";
            title = "Pull to Refresh";
        }
    );
    "1.1" =     (
                {
            detail = "Create custom stations of your favorite podcasts.";
            icon = Stations;
            title = "Custom Stations";
        }
    );
}

What I am trying to find out is the count of items within the sub Arrays. For example there are two sub arrays one for 1.1 and one for 1.2. I am looking to get the count of dictionaires from with those arrays?

Is there a quick way to access that. I have tried to complete a for loop and count them but I am getting NSCFString errors as if I have the data incorrect?

2

There are 2 best solutions below

2
On BEST ANSWER

Since you already mentioned trying a for loop yourself I don't know if this is actually the kind of answer you are looking for, but for this very specific purpose (i.e. you're certain that the input dictionaries always have the kind of structure you described) something along these lines should do the trick:

- (NSUInteger)numberOfItemsInSubArraysOfDictionary:(NSDictionary *)dict
{
    NSUInteger count = 0;
    for(NSArray *array in dict.allValues) {
        if ([array isKindOfClass:[NSArray class]]) {
            count += array.count;
        }
    }
    return count;
}
0
On

You can try something like this:

for (NSString *key in [dictionary allKeys]) {
    NSLog(@"Number of dictionaries for %@: %d", key, [dictionary[key] count]);
}

The output for this loop would be:

Number of dictionaries for 1.0: 2
Number of dictionaries for 1.1: 1