我正在开发一个基于 SwiftUI 文档的应用程序,其中包含一些易于序列化的数据和多个图像。我想将文档保存为一个包(即一个文件夹),其中一个文件包含易于序列化的数据,一个子文件夹包含图像作为单独的文件。我的包目录应该是这样的:
<UserChosenName.pspkg>/. // directory package containing my document data and images
PhraseSet.dat // regular file with serialized data from snapshot
Images/ // subdirectory for images (populated directly from my app as needed)
Image0.png
Image1.png
....
我创建了一个 FileWrapper 子类,它设置目录结构并适当地添加序列化快照,但是当我在 iOS 模拟器中运行应用程序并单击“+”创建一个新文档时,应用程序通过 PkgFileWrapper init() 运行并write() 没有错误,但没有明显地创建任何东西就返回浏览器窗口。我已经声明导出和导入类型标识符符合“com.apple.package”。任何人都可以建议一种方法来使其正常工作吗?
PkgFileWrapper 类如下所示:
class PkgFileWrapper: FileWrapper {
var snapshot: Data
init(withSnapshot: Data) {
self.snapshot = withSnapshot
let sWrapper = FileWrapper(regularFileWithContents: snapshot)
let dWrapper = FileWrapper(directoryWithFileWrappers: [:])
super.init(directoryWithFileWrappers: ["PhraseSet.dat" : sWrapper,
"Images" : dWrapper ])
// NOTE: Writing of images is done outside
// of the ReferenceFileDocument functionality.
}
override func write(to: URL,
options: FileWrapper.WritingOptions,
originalContentsURL: URL?) throws {
try super.write(to: to, options: options,
originalContentsURL: originalContentsURL)
}
required init?(coder inCoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}