在 cocoa 中保存文件

发布于 2024-09-03 18:25:35 字数 134 浏览 1 评论 0原文

我确信这是一个很容易回答的问题,但我对可可还是新手。我需要保存我的应用程序数据。该应用程序有 4 个文本字段,每个字段都需要保存到一个文件中。然后,当您打开文件时,它需要知道哪个字段中有什么内容。我真的被这个问题困扰了。另外,我确实知道如何使用保存面板。

I'm sure this is a really easy to answer question but I'm still new to cocoa. I need to save my applications data. The app has 4 text fields and each field needs to be saved into one file. Then when you open the file it needs to know what goes in what field. I'm really stuck with this. Also, I do know how to use the save panel.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

_失温 2024-09-10 18:25:35

一种方便的方法是使用 PLists

NSDictionary *arr = [NSDictionary dictionaryWithObjectsAndKeys:
                      string1, @"Field1", string2, @"Field2", nil];
NSData *data = [NSPropertyListSerialization dataFromPropertyList:arr
                format:NSPropertyListXMLFormat_v1_0 errorDescription:nil];

NSSavePanel *panel = [NSSavePanel savePanel];

NSInteger ret = [panel runModal];
if (ret == NSFileHandlingPanelOKButton) {
    [data writeToURL:[panel URL] atomically:YES];
}

用于反序列化:

NSData       *data = [NSData dataWithContentsOfURL:urlOfFile];
NSDictionary *dict = [NSPropertyListSerialization propertyListFromData:data
                       mutabilityOption:NSPropertyListImmutable
                       format:nil errorDescription:nil];
NSString *string1 = [dict objectForKey:@"Field1"];
// ... etc.

A convenient way would be to use PLists:

NSDictionary *arr = [NSDictionary dictionaryWithObjectsAndKeys:
                      string1, @"Field1", string2, @"Field2", nil];
NSData *data = [NSPropertyListSerialization dataFromPropertyList:arr
                format:NSPropertyListXMLFormat_v1_0 errorDescription:nil];

NSSavePanel *panel = [NSSavePanel savePanel];

NSInteger ret = [panel runModal];
if (ret == NSFileHandlingPanelOKButton) {
    [data writeToURL:[panel URL] atomically:YES];
}

For deserialization:

NSData       *data = [NSData dataWithContentsOfURL:urlOfFile];
NSDictionary *dict = [NSPropertyListSerialization propertyListFromData:data
                       mutabilityOption:NSPropertyListImmutable
                       format:nil errorDescription:nil];
NSString *string1 = [dict objectForKey:@"Field1"];
// ... etc.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文