简体   繁体   中英

Reusable NSMutableDictionary

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

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];

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM