Automatically instantiate singleton at launch

浪子不回头ぞ 提交于 2019-12-11 04:33:25

问题


I have a singleton Session that I want instantiated at application launch. How do I do that?

I'm using this method of creating the singleton: http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html


回答1:


In the first line of your didFinishLaunchingWithOptions method

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 [YourSingletonClass class]; // ADD THIS LINE

it will trigger initialize method initialization in your singleton class

+ (void) initialize {
     _innerInstance = [[YourSingletonClass alloc] init];
}



回答2:


If you access the singleton in the applicationDidFinishLaunching: method, they will should get set up.



来源:https://stackoverflow.com/questions/4359574/automatically-instantiate-singleton-at-launch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!