iPhone应用程序:我在哪里放置配置文件? [英] iPhone App : Where do I put a config file?
问题描述
在我的应用程序中,我有一个简单的ASCII文件,它存储了一些配置信息以及它使用和更改的其他小信息。我想用应用程序将该文件复制到iPhone。
In my app, I have a simple ASCII file which stores some config info and other small info which it uses and changes. I want to copy that file to the iPhone with the app.
1)请告诉我应该把这个文件(config.txt)放在xcode中。我应该把它放在资源下吗?
1) Please tell me where I should put this file (config.txt) in xcode. Should I put it under Resources ?
2)我将如何在iPhone中访问它?我可以使用
2) How will I access it in the iPhone ? Can I just use
str = [NSString stringWithContentsOfFile:@"config.txt"]
还是我必须使用更完整的路径;如果是,那是什么?
or do I have to use a more complete path; if yes, what is that ?
推荐答案
你应该使用 NSUserDefaults
来存储用户设置,如果应用程序可以更改它们。
该文档是此处。
You should use NSUserDefaults
to store user settings, if the application can change them.
The documentation is here.
设置存储为plist文件,因此您可以存储 NSDictionary
实例, NSArray
实例等。
The settings are stored as a plist file, so you can store NSDictionary
instances, NSArray
instances, etc.
如果你想预先填充你的 NSUserDefaults
通过一些设置,您可以使用以下代码执行此操作:
If you want to pre-populate your NSUserDefaults
with some settings, you can do so with some code like this one:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"defaults" ofType:@"plist"];
NSDictionary *defaultsDict = [NSDictionary dictionaryWithContentsOfFile:filePath];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultsDict];
您需要使用默认设置在Resources文件夹中放置defaults.plist文件,并使用上面的代码。我从AppDelegate的 +(void)初始化
方法运行该代码,但您可以选择另一个地方来调用它。
You need to put a defaults.plist file on your Resources folder with the default settings, and use the code above. I run that code from the AppDelegate's +(void)initialize
method, but you can choose another place to call it.
这篇关于iPhone应用程序:我在哪里放置配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!