开发者

Reusable NSMutableDictionary

开发者 https://www.devze.com 2022-12-18 19:50 出处:网络
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:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消