将文件保存在可可 [英] Saving files in cocoa

查看:122
本文介绍了将文件保存在可可的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信这是一个很容易回答的问题,但我还是新可可。我需要保存我的应用程序数据。该应用程序有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.

推荐答案

一种方便的方法是使用 PLists

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

反序列化:

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

这篇关于将文件保存在可可的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆