相关疑难解决方法(0)

在 SwiftUI 文档应用程序中,如何从函数内保存文档

当前版本的 Xcode(版本 12.5.1)为 macOS 的基于文档的应用程序提供了一个模板,提供以下文档模型:

struct MyDocument: FileDocument {
    var text: String

    init(text: String = "Hello, world!") {
        self.text = text
    }

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

    init(configuration: ReadConfiguration) throws {
        guard let data = configuration.file.regularFileContents,
              let string = String(data: data, encoding: .utf8)
        else {
            throw CocoaError(.fileReadCorruptFile)
        }
        text = string
    }
    
    func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
        let data = text.data(using: .utf8)!
        return .init(regularFileWithContents: data)
    }
}
Run Code Online (Sandbox Code Playgroud)

我想向此结构添加一个方法,将我的文档传递给外部程序,同时在执行此操作之前保存文档:

func passMyDocumentToProgram() {
    // Save document
    // Pass …
Run Code Online (Sandbox Code Playgroud)

macos document-based swift swiftui

7
推荐指数
1
解决办法
1897
查看次数

Dynamically hiding view in SwiftUI

I'm trying to conditionally hide a DatePicker in SwiftUI. However, I'm having any issue with mismatched types:

var datePicker = DatePicker($datePickerDate)
if self.showDatePicker {
    datePicker = datePicker.hidden()
}
Run Code Online (Sandbox Code Playgroud)

In this case, datePicker is a DatePicker<EmptyView> type but datePicker.hidden() is a _ModifiedContent<DatePicker<EmptyView>, _HiddenModifier>. So I cannot assign datePicker.hidden() to datePicker. I've tried variations of this and can't seem to find a way that works. Any ideas?

UPDATE

You can unwrap the _ModifiedContent type to get the underlying type using …

swift swiftui

5
推荐指数
7
解决办法
3851
查看次数

标签 统计

swift ×2

swiftui ×2

document-based ×1

macos ×1