启动时自动实例化单例 [英] Automatically instantiate singleton at launch
本文介绍了启动时自动实例化单例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个单例 Session
我想在应用程序启动时实例化。我该怎么做?
I have a singleton Session
that I want instantiated at application launch. How do I do that?
我正在使用这种创建单例的方法: http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html
I'm using this method of creating the singleton: http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html
推荐答案
在您的 didFinishLaunchingWithOptions
方法的第一行
- (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];
}
这篇关于启动时自动实例化单例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文