在我的 SwiftUI 应用程序中,我有许多
DocumentGroup
,其中许多设置为使用 newDocument:
的 init 的 DocumentGroup
变体进行编辑,可以正常构建和运行。
我还有两个
DocumentGroup
设置,仅使用 init 的 viewing:
变体进行查看。然而,当它们加在一起时无法构建:
DocumentGroup(viewing: ImageDocument.self) { config in
ImageViewer(content: config.$document, url: config.fileURL)
}
DocumentGroup(viewing: CustomPDFDocument.self) { config in
PDFViewer(content: config.$document, url: config.fileURL)
}
单独添加时,每个构建都没有语法错误,但是当我将它们添加在一起时,无论顺序如何,第二个都会引发
Extra argument in call
构建错误。这是截图
请注意,日志中的完整错误没有更多帮助:
/Users/<path redacted>/../App.swift:120:3: error: extra argument in call
DocumentGroup(viewing: ImageDocument.self) { config in
~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
请注意,视图已设置为使用以下 init 记录 URL,
init(content: Binding<ImageDocument>, url: URL?) {
_document = content
// ...
}
并返回完全有效的
View
(如上所述,每次添加一个时,它们都可以完美地构建和显示图像和 PDF 文档)。
我想这是因为我对 Swift 和 SwiftUI 缺乏了解。任何帮助将不胜感激。
啊,根据不同上下文中的答案,我尝试将它们放在
Group
中并且它们有效!
Group {
DocumentGroup(viewing: ImageDocument.self) { config in
ImageViewer(content: config.$document, url: config.fileURL)
}
DocumentGroup(viewing: CustomPDFDocument.self) { config in
PDFViewer(content: config.$document, url: config.fileURL)
}
}
也许这是重复的,因为它可以从其他答案中找出来。我很乐意把它留在这里,但是......