是否可以从特定方法初始化Singleton而不是在执行应用程序时? [英] Is it possible to initialize a Singleton from a specific method and not when application is executed?

查看:58
本文介绍了是否可以从特定方法初始化Singleton而不是在执行应用程序时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从特定方法初始化Singleton而不是在执行应用程序时?

Is it possible to initialize a Singleton from a specific method and not when application is executed?

我需要在viewdidload中运行一个方法,具体取决于Singleton是否返回nil或不是,因为它在应用程序运行时创建,然后它永远不会返回nil。

I need to run a method in viewdidload depending if Singleton returns nil or not but as it has being created when the app runs, then it never returns nil.

推荐答案

Singleton的getInstance()根据定义,永远不会返回 null 。如果尚未创建Singleton实例,则将创建它并返回对象引用。我猜你唯一一次得到 null 返回是因为某些原因导致对象创建失败(内存不足或某些不可预见的情况)。

The getInstance() of a Singleton, by definition will never return null. If the Singleton instance is not created yet, then it will be created and the object reference will be returned. I guess the only time you will get null back is if the object creation failed for some reason (out of memory or some such unforeseen circumstances).

这篇关于是否可以从特定方法初始化Singleton而不是在执行应用程序时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆