iphone 全局设置 - 实现它的最佳方法?
我想要一些可以从应用程序中的任何位置访问的设置。有没有最好的方法来实现这个?现在我只是将属性粘贴在我的应用程序委托中,然后通过以下方式访问它们:
ClientAppDelegate *appDelegate = (ClientAppDelegate *)[[UIApplication sharedApplication] delegate];
settingValue = appDelegate.setting;
I'd like to have some settings that I can access from anywhere in my app. Is there a best way to implement this? Right now I'm just sticking properties in my app delegate, then access them with:
ClientAppDelegate *appDelegate = (ClientAppDelegate *)[[UIApplication sharedApplication] delegate];
settingValue = appDelegate.setting;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果它们是持久的,请使用 NSUserDefaults< /a>.如果不是,请将它们包装在一个类中,并为每个需要它们的类提供一个指针。在每种情况下,您都应该能够更改与配置对象的连接,以便 (1) 依赖性变得明显(“啊哈,此代码的行为取决于配置”)并且 (2) 您可以提供自定义配置用于测试目的的对象。 Miško Hevery 撰写了一系列关于单例、耦合和测试的精彩文章。您可以从单身人士是病态骗子 并从那里跟进,这将对您的设计有好处。
If they are persistent, use NSUserDefaults. If they are not, wrap them in a class and give every class that needs them a pointer. In every case you should probably make it possible to change the connection to the configuration object so that (1) the dependency gets obvious (“aha, this code’s behaviour depends on the configuration”) and (2) you can supply a custom configuration object for testing purposes. There is a great series of articles about singletons, coupling and testing by Miško Hevery. You can start by the post called Singletons are Pathological Liars and follow up from there, it will do good to your design.
使用 NSUserDefaults - 它们是存储应用程序设置的可靠、简单的方法,甚至可以在应用程序启动之间保留。
Use
NSUserDefaults
—they're a reliable, simple way of storing application settings, and even persist between app launches.