Why is using a Global Variables class (singleton) bad practise?
This question already has an answer here:
A lot of objects in UIKit are singletons. The UIApplication object is a singleton. NSUserDefaults has the standardUserDefaults
singleton. UIDevice currentDevice
returns a singleton.
Just because it's a singleton doesn't mean it's bad. What is bad is when you start tying functionality in other classes to your singleton object, with it so deeply ingrained that you can't alter the singleton or the affected object easily.
I use a singleton to store my non-CoreData object structures. I also define some helper methods for getting the library directory, encoding data, and keyed archiving. So I can reference a master array object wherever I need it, or easily access methods that would otherwise just be copy and pastes.
Globals.m
doesn't sound really like a singleton, and if it is, it looks like it's just used for configuration purposes? Then a file full of #defines
or extern
would do the same, I guess. Without any more details it's hard to know what you're searching.
下一篇: 为什么使用全局变量类(单例)不好的做法?