iOS:在应用程序开始时初始化所有控制器使用的对象 [英] iOS: Initialise object at start of application for all controllers to use
问题描述
我需要的是一个在启动时初始化的对象,并且在应用程序关闭之前可供所有控制器使用。
该对象将保存从数据库(sql)或本地存储装载的数据 - 我不是100%确定的,但此处该做什么。
我需要所有viewController始终访问该数据存储对象,并且我需要在应用程序进入后台时保留该对象。
是否可以实现?如果,那么我会怎么做?
对于简单变量我知道我可以使用extern变量,但它也适用于完整对象吗?
感谢您的回答,
sebastian
这里更常见的问题。我建议远离 extern
变量和单身人士,请参阅我对此相关问题的回答和这个示例Xcode项目,以获得更好的解决方案。 (该示例项目目前非常简单,稍后我会添加更多常见方案。)
i'm working on a little iOS project, and i stumbled across a problem with variable scoping.
what i need is an object that is initialised at launchtime, and is available to all controllers until the application closes.
the object will hold data that is loaded either from a database (sql) or from local storage - im not 100% sure yet what to do here.
i need all viewControllers to access that data-holding object at all times, and i need the object to retain when the app enters the background.
is this possible to achieve? and if, then how would i do it?
for simple variables i know i can use extern variables, but does it also work for complete objects?
thanks for an answer, sebastian
This is one of the more common questions here. I would advise to stay away from extern
variables and singletons, see my answer for this related question and this sample Xcode project for a better solution. (The sample project is very bare-bones at the moment, I will add more common scenarios later.)
这篇关于iOS:在应用程序开始时初始化所有控制器使用的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!