在应用程序中存储iPhone应用程序设置 [英] Storing iPhone application settings in app

查看:153
本文介绍了在应用程序中存储iPhone应用程序设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的iPhone应用程式只有几个设定,使用者可能会经常变更。我想知道是否有任何建议的方式来处理这样的设置(阅读和保存)。在Apple网站上,我发现只有一个教程,介绍如何将应用设置与设置应用集成( link ),但我不希望用户退出我的应用程序,因此他只需更改选项。

My iPhone app has few settings that users is likely to change quite often. I would like to know if there's any suggested way of handling such settings (reading and saving them). On Apple sites I found only a tutorial about integrating your application settings with Settings app (link) but I don't want a user to exit my app so he could just change the option.

推荐答案

最好最简单的解决方案在iPhone中存储设置的方式是通过 NSUserDefaults 。让你不必处理文件系统或plists或任何其他的东西。

Best and easiest way to store settings in the iPhone is through NSUserDefaults. Keeps you from having to deal with the file system or plists or any of that other stuff.

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSString *storedVal = @"This is what you want to save";
NSString *key = @"storedVal"; // the key for the data

[defaults setObject:storedVal forKey:key];
[defaults synchronize]; // this method is optional


// Get the results out
NSString *results = [defaults stringForKey:key];

这里是苹果对可以存储在默认值

Here's what Apple says on the types of objects you can store in the Defaults


默认对象必须是属性
list,也就是说,一个实例(或者
)集合实例组合
of):NSData,NSString,NSNumber,
NSDate,NSArray或NSDictionary。如果
你想存储任何其他类型的
对象,你应该通常归档
它创建一个NSData的实例。

A default object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. If you want to store any other type of object, you should typically archive it to create an instance of NSData.

还有一些注意事项,如果你存储一个NSDictionary,键值必须是字符串。

There are some more caveats, like if you store an NSDictionary the key values must be strings.

这篇关于在应用程序中存储iPhone应用程序设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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