Q:

How to access local variable in for loop through dictionary or dictionaries when only one dictionary is available?

I have a for loop going through a dictionary of dictionaries. It works just fine unless there is only one dictionary total.
Here's the gist of my method:
- (NSArray *)objectsFromJSON:(NSString *)jsonString error:(NSError **)error
{
    NSParameterAssert(jsonString != nil);

    // create JSON object
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *localError = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&localError];
    NSDictionary *dictionaryOfDictionaries = (id)jsonObject;

    // if not parsed successfully, error
    if(dictionaryOfDictionaries == nil)
    {
        if(error != NULL)
        {
            *error = [NSError errorWithDomain:ErrorDomain code:MissingDataError userInfo:nil];
        }

        return nil;
    }

    // else create objects out of parsed data
    NSMutableArray *objectsArray = [NSMutableArray array];

    for(NSDictionary *objectDictionary in dictionaryOfDictionaries)
    {
        // required properties
        Obj *object = [[Obj alloc]
                            initWithName:objectDictionary[@"name"]
                            Street:objectDictionary[@"street"]
                            City:objectDictionary[@"city"]
                            State:objectDictionary[@"state"]
                          ];

        // if error, return
        if(object == nil)
        {
            if(!error)
            {
                *error = [NSError errorWithDomain:ErrorDomain code:MissingDataError userInfo:nil];
            }

            return nil;
        }

        // else add to array of objects created successfully
        [objectsArray addObject:object];
    }

    return [objectsArray copy];
}

As mentioned, this works fine - i.e. all of the objects are created successfully out of the objectDictionary - if there's more than one dictionary in dictionaryOfDictionaries. However, if there is just one dictionary, objectDictionary ends up being type id in the for loop (instead of type Obj), and trying to access its values, such as objectDictionary[@"name"] results in an error.
How do I most efficiently write this so the case of having one dictionary is handled?

A:

Here's the issue. When you have more than one dictionary, you actually get an array of dictionaries. In other words, jsonObject is really an NSArray containing each of the dictionaries.
When there is just one dictionary, jsonObject is just that one dictionary.
Change your code like this:
id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&localError];
NSArray *arrayOfDictionaries = nil;
if (jsonObject == nil) {
    // handle error
} else if ([jsonObject isKindOfClass:[NSArray class]]) {
    arrayOfDictionaries = jsonObject;
} else {
    arrayOfDictionaries = @[ jsonObject ];
}

Then change the for loop to:
for(NSDictionary *objectDictionary in arrayOfDictionaries)

