Home > Enterprise >  Unable to compile fileExporter view modifier in SwiftUI
Unable to compile fileExporter view modifier in SwiftUI

Time:04-15

I'm having some challenges in creating an export functionality for my hobby project. I want to be able to export my data in JSON format. According to the FileDocument capabilities, this should be just what I'm looking for.

I gathered my FileDocument and after setting the view modifier of .fileExporter and filling in the right information, it does not compile.

My code looks like this:

struct YourData: View {
    @ObservedObject var manager = Manager.shared
    @State private var isShowingFileExporter = false
    private var document: FileDocument? {
      manager.retrieveData()
    }
    var body: some View {
      List {
        Text("Import Data")
        Text("Export Data")
          .onTapGesture {
            isShowingFileExporter = true
          }
      }
      .navigationTitle("Manage Your Data")
      .navigationBarTitleDisplayMode(.inline)
      .fileExporter(
        isPresented: $isShowingFileExporter,
        document: document,
        contentType: .json,
        onCompletion: { result in
          switch result {
          case let .success(url):
            print("Data saved to: \(url)")
            isShowingFileExporter = false
          case let .failure(error):
            print("Data failed to save with error: \(error.localizedDescription)")
            isShowingFileExporter = false
          }
        }
      )
    }
}

The compile error states: no exact matches in call to instance method 'fileExporter' Am I missing an import or something? I only import SwiftUI and UniformTypeIdentifiers.

Any tips greatly appreciated.

CodePudding user response:

The .fileExporter expects concrete type of document (not a protocol FileDocument), so it should be provided

struct YourData: View {
    @ObservedObject var manager = Manager.shared
    @State private var isShowingFileExporter = false
    private var document: YourFileDocument? {             // << here !!
  • Related