Exporting a Document with FileDocument, Not Packaged

I'm trying to export a document file. It contains a codable struct named NoteGroup.

struct NoteGroup: Codable {
	let id: UUID
	let name: String
	let createAt: Date
	let children: [NoteChild]
	
	init(id: UUID = .init(), name: String = "", createAt: Date = .init(), children: [NoteChild]) {
		self.id = id
		self.name = name
		self.createAt = createAt
		self.children = children
	}
}

, which contains another object named NoteChild. I have a FileDocument struct as follows.

import SwiftUI
import UniformTypeIdentifiers

struct Document: FileDocument {
	var document: NoteGroup
	
	static var readableContentTypes = [UTType.frogType]
	
	init(document: NoteGroup = NoteGroup(children: [NoteChild(id: UUID(), name: "", createAt: Date())])) {
		self.document = document
	}
	
	init(configuration: ReadConfiguration) throws {
		self.init()
	}
	
	func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
		do {
			let data = try getDocumentData()
			let jsonFileWrapper = FileWrapper(regularFileWithContents: data)
			let filename = "Note.frog"
			jsonFileWrapper.filename = filename
			let fileWrapper = FileWrapper(directoryWithFileWrappers: [filename: jsonFileWrapper])
			return fileWrapper
		} catch {
			throw error
		}
	}
	
	private func getDocumentData() throws -> Data {
		let encoder = JSONEncoder()
		do {
			let data = try encoder.encode(document)
			return data
		} catch {
			throw error
		}
	}
}

extension UTType {
	public static let frogType = UTType(exportedAs: "com.example.frog")
}

And I export a file like the following.

import SwiftUI
import UniformTypeIdentifiers

struct ContentView: View {
	@State private var showingExporter = false
	@State var doc = Document()
	
	var body: some View {
		VStack {
			Button("Tap to export") { 
				showingExporter.toggle()
			}
			.fileExporter(
				isPresented: $showingExporter,
				document: doc,
				contentType: .frogType
			) { result in
				switch result {
				case .success(let file):
					print(file)
				case .failure(let error):
					print(error)
				}
			}
		}.onAppear { 
			doc = Document(document: NoteGroup(id: UUID(), name: "Kyle", createAt: Date(), children: [NoteChild(id: UUID(), name: "Nancy", createAt: Date())]))
		}
    }
}

Well, I have read this topic. And I've watched this video about Uniform Type Identifiers. Thanks to the video, I am able to export a file. Yet, I end up with a folder (Frog.frog), not a packaged file. There is a JSON file in it, though. What am I doing wrong? It's for iOS. La vida no es facil. Muchos thankos.

Answered by Tomato in 762074022

Silly me...

	func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
		let fileContent = try JSONEncoder().encode(self.document)
		return FileWrapper(regularFileWithContents: fileContent)
	}

Hmm... Maybe

let fileWrapper = FileWrapper(directoryWithFileWrappers: [filename: jsonFileWrapper])

is wrong?

Accepted Answer

Silly me...

	func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
		let fileContent = try JSONEncoder().encode(self.document)
		return FileWrapper(regularFileWithContents: fileContent)
	}
Exporting a Document with FileDocument, Not Packaged
 
 
Q