UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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?

2      

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)
    }
}

2      

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!

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.