xcode iPhone 应用程序:在哪里放置配置文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/789397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iPhone App : Where do I put a config file?
提问by euphoria83
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.
在我的应用程序中,我有一个简单的 ASCII 文件,它存储了一些配置信息和它使用和更改的其他小信息。我想使用该应用程序将该文件复制到 iPhone。
1) Please tell me where I should put this file (config.txt) in xcode. Should I put it under Resources ?
1) 请告诉我应该把这个文件 (config.txt) 放在 xcode 的什么位置。我应该把它放在资源下吗?
2) How will I access it in the iPhone ? Can I just use
2) 我将如何在 iPhone 中访问它?我可以用吗
str = [NSString stringWithContentsOfFile:@"config.txt"]
or do I have to use a more complete path; if yes, what is that ?
还是我必须使用更完整的路径;如果是,那是什么?
回答by pgb
You should use NSUserDefaults
to store user settings, if the application can change them.
The documentation is here.
NSUserDefaults
如果应用程序可以更改它们,您应该使用它来存储用户设置。文档在这里。
The settings are stored as a plist file, so you can store NSDictionary
instances, NSArray
instances, etc.
设置存储为 plist 文件,因此您可以存储NSDictionary
实例、NSArray
实例等。
If you want to pre-populate your NSUserDefaults
with some settings, you can do so with some code like this one:
如果您想预先填充NSUserDefaults
一些设置,您可以使用如下代码:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"defaults" ofType:@"plist"];
NSDictionary *defaultsDict = [NSDictionary dictionaryWithContentsOfFile:filePath];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultsDict];
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.
您需要使用默认设置在您的资源文件夹中放置一个 defaults.plist 文件,并使用上面的代码。我从 AppDelegate 的+(void)initialize
方法运行该代码,但您可以选择另一个地方来调用它。
回答by xil3
And if you want to retrieve the NSUserDefaults, you can do the following:
如果您想检索NSUserDefaults,您可以执行以下操作:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting a NSString
NSString *myString = [prefs stringForKey:@"keyToLookupString"];
// getting a NSInteger
NSInteger myInt = [prefs integerForKey:@"integerKey"];
// getting a Float
float myFloat = [prefs floatForKey:@"floatKey"];
回答by S?ren Kuklau
You can put it in Resources, yes. To get the file, then, you can simply use:
你可以把它放在资源中,是的。然后,要获取文件,您只需使用:
[[NSBundle mainBundle] pathForResource:@"config" ofType:@"txt" inDirectory:@""]]
May I suggest NSUserDefaults
for your settings, however? It will save you plenty of trouble in reading and writing them.
NSUserDefaults
但是,我可以建议您的设置吗?它将为您在阅读和编写它们时省去很多麻烦。
回答by Mark Bessey
If nsuserdefaults doesn't meet your needs, you could store the config information in a file in the Documents directory. If the file doesn't exist on startup, then read the version you have in Resources.
如果 nsuserdefaults 不能满足您的需要,您可以将配置信息存储在 Documents 目录中的文件中。如果该文件在启动时不存在,请阅读资源中的版本。