ios - 创建跨类的全局对象

标签 ios ios5 object global-variables

我需要创建一个全局对象,该对象可以工作并且可以在程序中的所有类中使用。我做了一些研究,发现该解决方案似乎是在 AppDelegate 中实现的,但似乎没有太多关于如何实现这一点的解释,更重要的是,根据我的理解,这似乎并不正确AppDelegate 的目的。

最佳答案

您应该检查singleton pattern :

In software engineering, the singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects. The term comes from the mathematical concept of a singleton.

这里是示例实现的来源:What should my Objective-C singleton look like?

关于ios - 创建跨类的全局对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11608104/

相关文章:

javascript - 循环访问对象名称时出现问题

ios - 如何更改输入的文本颜色?

iphone - CLGeoCoder 导致应用程序崩溃?

ios - 隐式转换丢失整数精度 : 'long long' to 'NSInteger' (aka 'int' )

iphone - 目前 Siri 开放 API 可用吗?

Java - 定义一个扩展类 A 并实现接口(interface) B 的成员

javascript - 通过字符串路径访问和修改 JSON-Object

ios - 从 coredata 获取父子数组

ios - CLLocationmanager - 纬度和经度的小数点

ios - Flutter -ios - pod install gRPC-C++ sed ssl 问题