gpt4 book ai didi

objective-c - NSArrayController + NSTableView : automatically save changes without Core Data

转载 作者:行者123 更新时间:2023-12-03 17:14:44 25 4
gpt4 key购买 nike

好的,我正在实现一个经典场景:

  • 一个 NSPopupButton ,其中包含一些项目
  • 当所选值更改时,我的 itemsArray 会更新
  • itemsArray 链接到 NSArrayController
  • itemsArray 中的每个项目都是一个 NSMutableDictionary(带有键:titlecontent)
  • NSTableView 显示 arrangedObjectstitle(绑定(bind))
  • NSTextView 显示所选项目的内容

现在,我想要的是自动保存对 itemsArray (或 itemsArray 的项目标题/内容)的任何更改,但不使用核心数据(我怀疑这可能是最好的方法)。

我想这是一个非常基本的问题,但老实说,我从来没有真正喜欢过 Cocoa 的自动神奇的做事方式......所以,我需要你的帮助......

我应该怎么做?

最佳答案

您可以非常轻松地将数组写入文件:

[yourArray writeToURL:someFileURL atomically:YES];

如果数组的所有内容都是属性列表对象(即它们是 NSNumberNSStringNSDictionaryNSArrayNSData 对象)。您的示例就是这种情况。

然后,当您从磁盘加载时,您可以使用 arrayWithContentsOfURL:initWithContentsOfURL: 方法重新创建数组。

如果您的模型对象比数组更复杂,那么您应该使您的模型对象符合 NSCoding 协议(protocol)。这意味着您需要实现 initWithCoder:encodeWithCoder: 方法。然后,您可以使用 NSKeyedArchiverNSKeyedUnarchiver 类将对象与可写入磁盘的 NSData 表示形式相互转换。

您应该阅读Archives and Serialization Programming Guide了解更多详细信息。

关于objective-c - NSArrayController + NSTableView : automatically save changes without Core Data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10119125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com