菜鸟教程小白 发表于 2022-12-13 17:21:44

ios - appDelegate 实践


                                            <p><p>我的应用文档文件夹中存储了一个 json,我需要在我的所有 View 中使用它。我正在加载 json 并将其添加到每个 View 中的 NSMutableArray 中。但是现在我了解到,我可以简单地将数组加载到 AppDelegate 内部的 NSMutableArray 中,并在需要时直接从 AppDelegate 获取信息。</p>

<p>这是一种不好的做法吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我最好使用 <a href="http://en.wikipedia.org/wiki/Singleton_pattern" rel="noreferrer noopener nofollow">Singleton</a>对于您的 <code>JSON 存储</code>,而不是 <code>AppDelegate</code>。</p>

<p>因此您可以在代码中的任何位置使用它,例如:</p>

<pre><code>[ dataArray];
</code></pre>

<p>随着项目的发展,您可以向此类添加新方法</p>

<p>这是 <a href="http://www.johnwordsworth.com/2010/04/iphone-code-snippet-the-singleton-pattern/" rel="noreferrer noopener nofollow">article</a>如何在 Objective-C 中实现单例</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - appDelegate 实践,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/13016402/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/13016402/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - appDelegate 实践