Two DocumentGroups with Viewing: Resolving the "Extra Argument in Call" Build Error
In this article, we'll tackle a common SwiftUI issue encountered when using multiple DocumentGroup
instances. This problem, as described on Stack Overflow by user 'TheLazyOne', surfaces when using the viewing:
variant of the DocumentGroup
initializer, resulting in an "Extra argument in call" error.
Let's break down the problem and provide a solution.
The Problem:
The user has two DocumentGroup
instances, both configured for viewing specific document types (ImageDocument
and CustomPDFDocument
). When these groups are defined separately, they build without issues. However, combining them leads to the "Extra argument in call" error.
Root Cause:
The issue stems from a misunderstanding of how SwiftUI's DocumentGroup
works. The viewing:
initializer, designed for viewing documents, allows only one active DocumentGroup
at a time within an app. Attempting to declare multiple viewing
based DocumentGroup
s essentially creates a conflict, leading to the build error.
Solution:
Instead of attempting to define multiple DocumentGroup
s for viewing, consider using a single DocumentGroup
to handle both document types. You can achieve this by:
-
Creating a common base class or protocol: Define a base class or protocol for your documents (
ImageDocument
andCustomPDFDocument
) to share common properties. -
Unified Viewer: Implement a single
Viewer
that can handle both document types. This viewer should accept the common base class or protocol as a parameter. -
Single
DocumentGroup
: Configure a singleDocumentGroup
using theviewing:
initializer, with your common base class/protocol as the type. ThisDocumentGroup
will handle both document types.
Code Example:
Let's assume a Document
protocol for our document types:
protocol Document {
var data: Data { get } // Example: Common property
}
class ImageDocument: Document {
// ...
}
class CustomPDFDocument: Document {
// ...
}
And a UniversalViewer
to handle both document types:
struct UniversalViewer: View {
@Binding var document: Document
let url: URL?
var body: some View {
if let imageDocument = document as? ImageDocument {
ImageViewer(content: $imageDocument, url: url)
} else if let pdfDocument = document as? CustomPDFDocument {
PDFViewer(content: $pdfDocument, url: url)
} else {
Text("Unsupported document type")
}
}
}
Finally, the single DocumentGroup
for viewing:
DocumentGroup(viewing: Document.self) { config in
UniversalViewer(content: config.$document, url: config.fileURL)
}
Key Points:
- This solution leverages polymorphism and a unified viewer to handle multiple document types within a single
DocumentGroup
. - Using a single
DocumentGroup
ensures that your app correctly handles document viewing without the "Extra argument in call" build error.
By implementing this approach, you can effectively handle viewing multiple document types in your SwiftUI application. Remember, always strive for a cohesive and unified approach when dealing with different document types, leading to cleaner and more maintainable code.