【发布时间】:2020-08-06 09:15:24
【问题描述】:
如何改进?当我使用ForEach(CoreData 实体)时,选择器返回空字符串。如果我使用 testing[String] 一切正常
import SwiftUI
struct AddTask: View {
@Environment(\.managedObjectContext) var moc
@FetchRequest(entity: Goal.entity(), sortDescriptors: []) var goals: FetchedResults<Goal>
@State private var taskName: String = ""
@State private var originGoal = ""
private let testGoal = ["Alpha", "Bravo", "Charlie"]
var body: some View {
NavigationView {
Form {
Section{
TextField("Enter Task Name", text: $taskName)
}
Section {
Picker(selection: $originGoal, label: Text("Choose Goal")) {
ForEach(goals, id: \.self) { goal in
Text(goal.wrappedName)
}
}
}
}
}
Button("Add") {
let task1 = Task(context: self.moc)
task1.name = taskName
task1.origin = Goal(context: self.moc)
task1.origin?.name = originGoal
try? self.moc.save()
}
}
}
或者也许 SwiftUI Picker 有不错的替代品?
【问题讨论】:
-
Picker return empty String是什么意思? -
选择器不要从目标列表中选择任何变体,显示,但不要选择