开发者

iphone global settings - best way to implement it?

I'd like to have some settings th开发者_如何转开发at I can access from anywhere in my app. Is there a best way to implement this? Right now I'm just sticking properties in my app delegate, then access them with:

ClientAppDelegate *appDelegate = (ClientAppDelegate *)[[UIApplication sharedApplication] delegate];
settingValue = appDelegate.setting;


If they are persistent, use NSUserDefaults. If they are not, wrap them in a class and give every class that needs them a pointer. In every case you should probably make it possible to change the connection to the configuration object so that (1) the dependency gets obvious (“aha, this code’s behaviour depends on the configuration”) and (2) you can supply a custom configuration object for testing purposes. There is a great series of articles about singletons, coupling and testing by Miško Hevery. You can start by the post called Singletons are Pathological Liars and follow up from there, it will do good to your design.


Use NSUserDefaults—they're a reliable, simple way of storing application settings, and even persist between app launches.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜