可重用的 NSMutableDictionary

发布于 2024-08-20 07:19:27 字数 370 浏览 5 评论 0原文

该论坛中的某人向我提出了一个效果很好的代码解决方案,但根据我的理解,我想知道这两个代码块之间有什么区别:

块 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞慾 2024-08-27 07:19:27

没有任何。编译器会优化 step_info = nil 并留下完全相同的代码。

以下是您可以采取的另一种方法:

NSMutableDictionary *step_info;
step_info = [NSMutableDictionary dictionary];

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];
◇流星雨 2024-08-27 07:19:27

首先使用 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 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];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文