iOS:使用应用程序委托来容纳全局变量 [英] iOS: Using the app delegate to house global variables

查看:123
本文介绍了iOS:使用应用程序委托来容纳全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是不好的做法是容纳我想从应用程序委托中的所有类访问的变量,并通过 [[UIApplication sharedApplication] delegate] someProperty]

Is it bad practise to house variables that I want to be accessible from all classes in the app delegate and get them via [[UIApplication sharedApplication] delegate] someProperty]

推荐答案

你的问题的真正解决方案不是用另一种形式的全局状态替换(即单例)。

The real solution to your question isn't to replace one form of global state with another (i.e. singletons).

你应该做的是将状态放入在你的应用程序代理中实例化的模型类中,然后将它们传递到你需要它们的应用程序部分控制器),从而完全消除您的全局状态问题。

What you should be doing is putting state within "model" classes that are instantiated inside your app delegate, and then passing them down to the parts of your application that need them (e.g. view controllers), thereby eliminating your global state problem entirely.

这篇关于iOS:使用应用程序委托来容纳全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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