开发者

iphone global settings - best way to implement it?

开发者 https://www.devze.com 2022-12-20 11:56 出处:网络
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 acce

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

精彩评论

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

关注公众号