I usually do option B as it allows me to do some debugging on the dictionary if I need to (as shown in option C). It also looks cleaner in my opinion.
This might probably be a non-issue but I’m wondering if options B or C use more memory or if there is even something else to consider when doing this in iOS programming. By the way, I’m using ARC.
Is there an advantage on using option A as opposed to option B or C?
// OPTION A
NSString *someString = [[self grabDictionaryFromDB] objectForKey:@"someField"];
// OPTION B
NSDictionary *dbRow = [self grabDictionaryFromDB]; // dbRow ONLY gets used in the next line
NSString *someString = [dbRow objectForKey:@"someField"];
// OPTION C
NSDictionary *dbRow = [self grabDictionaryFromDB]; // dbRow ONLY gets used in the next two lines
NSLog(@"Row: %@", dbRow);
NSString *someString = [dbRow objectForKey:@"someField"];
Chances are that options A and B will be optimized into exactly the same output by the compiler, so the only thing that you gain with option B is readability. Option C will likely require an extra store instruction and a pair of
[retain]/[release]from the ARC, but you would not notice the impact compared to options A or B.