NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

SOLVED: DocumentGroup and Settings

Forums > SwiftUI

Started a rewrite of my app using the newest SwiftUI and Xcode (Version 14.0 beta 2 (14A5229c))

Out of the box, it appears Settings is not compatible with DocumentGroup. At least I can't get it to work.

Any examples?

   

You mean on iOS? There Settings is not available. But on macOS I didn't find any problem. Do you test on macOS Ventura?

The app below does work for me in Xcode 14 beta 2 and macOS Monterey.

import SwiftUI
import UniformTypeIdentifiers

@main
struct MyApp: App {
    var body: some Scene {
        DocumentGroup(newDocument: MyDocument()) { configuration in
            ContentView(document: configuration.$document)
        }

        #if os(macOS)
        Settings {
            GeneralSettings()
        }
        #endif
    }
}

struct GeneralSettings: View {
    var body: some View {
        VStack {
            Text("Hello world")
                .padding()
        }
        .padding()
        .fixedSize()
    }
}

struct ContentView: View {
    @Binding var document: MyDocument
    @Environment(\.undoManager) var undoManager

    var body: some View {
        TextEditor(text: $document.text)
            .toolbar {
                ToolbarItemGroup(placement: .automatic) {
                    if let undoManager = undoManager {
                        Button(action: undoManager.undo) {
                            Label("Undo", systemImage: "arrow.uturn.backward")
                        }
                        .disabled(!undoManager.canUndo)

                        Button(action: undoManager.redo) {
                            Label("Redo", systemImage: "arrow.uturn.forward")
                        }
                        .disabled(!undoManager.canRedo)
                    }
                }
            }
    }
}

struct MyDocument: FileDocument {
    var text: String

    init(text: String = "Hello, world!") {
        print("⚫️ ", #function)
        self.text = text
    }

    static var readableContentTypes: [UTType] { [.plainText] }

    init(configuration: ReadConfiguration) throws {
        guard let data = configuration.file.regularFileContents,
              let string = String(data: data, encoding: .utf8)
        else {
            throw CocoaError(.fileReadCorruptFile)
        }
        print("⚫️ ", #function)
        text = string
    }

    func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
        let data = text.data(using: .utf8)!
        return .init(regularFileWithContents: data)
    }
}

   

Thank you.

Since you had no problem I went back and reviewed everything - my mistake. Really DUMB mistake.

I have set a data structure to hold my settings so each document could have it's own set. Guess what I called it! Sigh. "Settings".

So, I changed that and evcerything worked fine. Thank you for telling me it should want. That drove me to go back to basics. DUH!

   

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.