views:

237

answers:

2

Someone in that forum proposed me a code solution that worked great, but for my understanding, I would like to know what is the difference between the 2 blocks of code:

Block 1

NSMutableDictionary* step_info = [NSMutableDictionary dictionary];

Block 2

NSMutableDictionary* step_info = nil;
step_info = [NSMutableDictionary dictionary];

It is may be also important to mention that step_info has to be filled and reuse repeatedly to load into another NSmutabledictionary.

Thank's for your help

+2  A: 

None. The compiler optimises step_info = nil away and you're left with the exact same code.

The following is another approach you could take:

NSMutableDictionary *step_info;
step_info = [NSMutableDictionary dictionary];
Georg
Ok, here my question again:If I used that line code step_info = [NSMutableDictionary dictionary];many times at different places in my program, what does it do with my step_info NSMutabledicstionnary ?
@gpsdev: The previous `step_info` dictionaries are no longer accessible once overridden, filling up the autorelease pool.
KennyTM
A: 

Having NSMutableDictionary* step_info; first allows you to use step_info = [NSMutableDictionary dictionary] later in the same block of code.

If you wish to assign a value to step_info in multiple methods, it'd be better if you add NSMutableDictionary* step_info in the @interface section of the header file.

That way you can use step_info = [[NSMutableDictionary alloc] init] in any method in your implementation file, then assign values and keys this way: [step_info setValue: value forKey: key];

Cirrostratus