可重用的 NSMutableDictionary
该论坛中的某人向我提出了一个效果很好的代码解决方案,但根据我的理解,我想知道这两个代码块之间有什么区别:
块 1
NSMutableDictionary* step_info = [NSMutableDictionary dictionary];
块 2
NSMutableDictionary* step_info = nil;
step_info = [NSMutableDictionary dictionary];
可能还需要提到的是,step_info 必须被填充并重复使用以加载到另一个 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有任何。编译器会优化
step_info = nil
并留下完全相同的代码。以下是您可以采取的另一种方法:
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]
。如果您希望在多个方法中为step_info赋值,最好在头文件的
@interface
部分添加NSMutableDictionary* step_info
。这样,您就可以在实现文件中的任何方法中使用step_info = [[NSMutableDictionary alloc] init],然后按以下方式分配值和键:
[step_info setValue: value forKey: key];
Having
NSMutableDictionary* step_info;
first allows you to usestep_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];