iOS应用程序配置文件
在 C#应用程序中,我总是使用 app.config
文件来保存我的应用程序的一些数据,以便在需要时加载(例如,连接字符串) )。
In a C# application I always used an app.config
file to save some data for my application to load when needed (e.g Connection String).
在iOS应用程序中保存此类信息的最佳方法是什么?
What is the best way to save information like this in an iOS application?
你可以创建一个属性列表文件(在XCode中有一个模板,只有文件 - >新文件并选择那里),所以你会有类似settings.plist的东西,或类似的东西那。您可以将plist视为key => value配置文件,区别在于除了纯文本作为值之外,您还可以保存数组和字典。
You can create a property list file (there is a template for that in XCode, just to to File -> New file and choose there), so you will have something like "settings.plist", or anything like that. You can think of a plist as being a key => value config file, with the difference that you can also hold arrays and dictionaries, besides plain text as value.
使用NSBundle在您的应用中加载文件,例如
Use NSBundle to load the file in your app, like
NSString *path = [[NSBundle mainBundle] pathForResource:@"settings" ofType:@"plist"];
NSDictionary *settings = [[NSDictionary alloc] initWithContentsOfFile:path];
然后,您可以将您的密钥作为常规字典访问。只是不要忘记[释放]之后;)。
You can then access your keys as a regular dictionary. Just don't forget to [release] it after ;).