如何从 xcode plist 读取/写入?

发布于 2024-11-07 15:42:02 字数 873 浏览 0 评论 0原文

我有一个带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星光不落少年眉 2024-11-14 15:42:02

使用 NSUserDefaults。这就是您应该用来保存用户首选项的内容。

Use NSUserDefaults. That's what you're supposed to use for saving user preferences.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文