当前版本的 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) 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 …