如何使用先前保存的 coreData 加载 TextEditor
How to load TextEditor with previously saved coreData
我正在尝试使用 SwiftUI 构建一个简单的笔记样式应用程序。我设法创建了一个实体 (ApplicationQuestion
),使用 TextEditor
将用户输入 (question.answer
) 保存到该实体的一个属性并将该属性显示到视图。
但是,我希望能够编辑它,而不是每次都保存并覆盖 question.answer
。
我只能找到使用 UIkit 执行此操作的示例,文档没有解释如何将文本加载到编辑器中。如果这无法通过 SwiftUI 实现,我无法解决,我需要使用不同的方法。
答案编写器目前看起来像这样:
struct answerComposer: View {
let question: ApplicationQuestion
@Environment(\.managedObjectContext) var moc
@State var answerText: String = ""
@State private var currentWordCount: Int = 0
var body: some View {
VStack{
//Title
Text(self.question.title?.uppercased() ?? "Unknown Title")
.font(.largeTitle)
//Detail
Text(self.question.detail ?? "Unknown detail")
.font(.footnote)
.fontWeight(.light)
//answer
Text(self.question.answer ?? "Unknown answer")
.font(.body)
.padding()
//text editor
TextEditor(text: $answerText)
.allowsTightening(true)
.onChange(of: answerText) { value in
let words = answerText.split { [=10=] == " " || [=10=].isNewline }
self.currentWordCount = words.count
}
.padding()
// Save Button
Button("Save") {
// let question = ApplicationQuestion(context: self.moc)
question.answer = self.answerText
try? self.moc.save()
}
}
}
}
我猜你想要这个
struct answerComposer: View {
let question: ApplicationQuestion
@Environment(\.managedObjectContext) var moc
@State private var currentWordCount: Int = 0
@State private var answerText: String
init(question: ApplicationQuestion) {
self.question = question
self._answerText = State(initialValue: question.answer ?? "") // << here !!
}
// ... other code
}
我正在尝试使用 SwiftUI 构建一个简单的笔记样式应用程序。我设法创建了一个实体 (ApplicationQuestion
),使用 TextEditor
将用户输入 (question.answer
) 保存到该实体的一个属性并将该属性显示到视图。
但是,我希望能够编辑它,而不是每次都保存并覆盖 question.answer
。
我只能找到使用 UIkit 执行此操作的示例,文档没有解释如何将文本加载到编辑器中。如果这无法通过 SwiftUI 实现,我无法解决,我需要使用不同的方法。
答案编写器目前看起来像这样:
struct answerComposer: View {
let question: ApplicationQuestion
@Environment(\.managedObjectContext) var moc
@State var answerText: String = ""
@State private var currentWordCount: Int = 0
var body: some View {
VStack{
//Title
Text(self.question.title?.uppercased() ?? "Unknown Title")
.font(.largeTitle)
//Detail
Text(self.question.detail ?? "Unknown detail")
.font(.footnote)
.fontWeight(.light)
//answer
Text(self.question.answer ?? "Unknown answer")
.font(.body)
.padding()
//text editor
TextEditor(text: $answerText)
.allowsTightening(true)
.onChange(of: answerText) { value in
let words = answerText.split { [=10=] == " " || [=10=].isNewline }
self.currentWordCount = words.count
}
.padding()
// Save Button
Button("Save") {
// let question = ApplicationQuestion(context: self.moc)
question.answer = self.answerText
try? self.moc.save()
}
}
}
}
我猜你想要这个
struct answerComposer: View {
let question: ApplicationQuestion
@Environment(\.managedObjectContext) var moc
@State private var currentWordCount: Int = 0
@State private var answerText: String
init(question: ApplicationQuestion) {
self.question = question
self._answerText = State(initialValue: question.answer ?? "") // << here !!
}
// ... other code
}