我是Swift (5)和Cocoa开发的新手,试图开发一个基于NSDocument的应用程序。在我的文档(来自NSDocument的子类)中,我有几个属性、一个枚举、一个int和一个对象数组。对象实现NSCoding协议。应用程序还实现了打印,因此我想保存/加载NSPrintInfo属性(printInfo在NSDocument基类上)。
我的问题是如何实现数据:ofType函数覆盖到accompish保存:
在我看到的示例中,使用了NSKeyedArchiver,但是这只是从给定的单个根对象创建一个NSData对象,而我有多个要存储的对象。我可以创建一个新的数据对象来保存我想要存储的所有属性,但是应该如何引用NSDocument上的NSDocument属性呢?
发布于 2020-04-15 16:06:15
仅供任何阅读此内容的人参考;我确实通过以下方式解决了这个问题:-创建一个自定义类来保存我的数据和一个NSPrintInfo副本的NSDocument printInfo成员-覆盖printInfo变量在NSDocument类上添加一个didSet,该didSet将NSDocument printInfo复制到我的自定义数据对象上。
class MyDocument: NSDocument, NSKeyedUnarchiverDelegate
{
/// The printing information associated with the document.
override var printInfo: NSPrintInfo
{
didSet
{
// Update the data object used to save data
myData.printInfo = printInfo
}
}
}printInfo复制回NSDocument重写func读(从数据:数据,ofType typeName: String )抛出{
如果让NSKeyedUnarchiver.unarchiveTopLevelObjectWithData( = loadedData =加载为?MyData { //从加载的数据myData = loadedData //更新数据对象,现在更新本地printInfo undoManager?.disableUndoRegistration() printInfo = myData.printInfo undoManager?.enableUndoRegistration() }或{抛出MyError.BadData }
}
printInfo之前禁用撤消管理器将防止此代码将文档标记为陈旧文档,因此需要保存https://stackoverflow.com/questions/60850651
复制相似问题