iPhone应用程序:我在哪里放置配置文件?

时间:2021-02-11 12:22:23

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 ?

还是我必须使用更完整的路径;如果有,那是什么?

4 个解决方案

#1


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.

您需要使用默认设置在Resources文件夹上放置defaults.plist文件,并使用上面的代码。我从AppDelegate的+(void)初始化方法运行该代码,但您可以选择另一个地方来调用它。

#2


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吗?它将为您在阅读和编写时节省大量的麻烦。

#3


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"];

#4


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目录中的文件中。如果启动时文件不存在,请阅读参考资料中的版本。

#1


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.

您需要使用默认设置在Resources文件夹上放置defaults.plist文件,并使用上面的代码。我从AppDelegate的+(void)初始化方法运行该代码,但您可以选择另一个地方来调用它。

#2


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吗?它将为您在阅读和编写时节省大量的麻烦。

#3


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"];

#4


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目录中的文件中。如果启动时文件不存在,请阅读参考资料中的版本。