我应该如何或在哪里存储我需要在我的iOS应用程序全局对象实例? [英] How or where should I store object instances that I require globally within my iOS app?

查看:128
本文介绍了我应该如何或在哪里存储我需要在我的iOS应用程序全局对象实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个iOS应用程式。大多数应用程序需要访问持久性对象。当应用程序通过应用程序代理加载时,此对象被实例化。

I'm building an iOS application. Most of the application requires access to a persistent object. This object is instantiated when the app loads via the Application Delegate.

我遇到的问题是需要访问此对象的许多视图控制器。

The problem I have is that numerous View Controllers that need to access this object.

什么是最好的方式和最佳实践,创建全局对象,可以从应用程序中的任何地方访问?

What is the best way and best practice to create global objects that can be access from anywhere in the application?

。非常感谢。

推荐答案

您可能需要查看 Singleton 模式。

You might want to look at the Singleton pattern. The linked article provides a pretty good description of it including how to implement one in Cocoa.

如果Singleton在你的上下文中没有意义,你还需要全局引用你的变量,你可以在 AppDelegate 中引用它。 (不推荐

If the Singleton doesn't make sense in your context, and you still need the Global reference to your variable, you could just put a reference to it in your AppDelegate. (Not recommended)

可以随时通过以下方式从您的应用程序中访问:

It can be accessed from within your application anytime using:

[UIApplication sharedApplication] delegate]

这篇关于我应该如何或在哪里存储我需要在我的iOS应用程序全局对象实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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