如何从 xcode plist 读取/写入?
我有一个带有 Flipsideview 的实用程序应用程序,我正在尝试使用 Flipsideview 来使用户能够更改影响主视图的设置。我认为最简单的方法是将背面视图上所做的更改写入 plist,但我是 Objective-C 的新手,似乎无法在任何论坛中找到任何帮助。当我使用在网上找到的代码时,我不断收到“初始值设定项元素不是编译时常量”。这是代码:
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Settings.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"settings" ofType:@"plist"];
[fileManager copyItemAtPath:bundle toPath: path error:&error];
}
另外,我是 Objective-C 的新手(从 html、css 等过渡),因此对正在发生的事情的描述将非常感激。并且请不要只粘贴苹果开发人员教程的链接。我已经经历过它们,但似乎无法理解它,因为我通过做一个实际的例子学得更好。
提前致谢。
I have a utility app with the flipsideview and I'm trying to use the flipsideview to enable the user to change settings that will effect the main view. I thought the easiest way to do this would be to write changes made on the flip side view to a plist but I am new to objective-c and can't seem to find any help in any forums. When I use the code I found online I keep getting a "Initializer element is not a compile-time constant". This is the code:
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Settings.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"settings" ofType:@"plist"];
[fileManager copyItemAtPath:bundle toPath: path error:&error];
}
Also, I am new to objective-c (making the transition from html, css, etc.) and so a description of what's going on would be much appreciated. And please don't just paste a link to the apple developer tutorials. I've been through them and can't seem to grasp it being I learn much better by doing a practical example.
Thanks ahead of time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 NSUserDefaults。这就是您应该用来保存用户首选项的内容。
Use
NSUserDefaults
. That's what you're supposed to use for saving user preferences.