I have the following code:
import SwiftUI
struct LearnView: View {
@State private var selectedLanguage: Language?
@State private var selectedCategory: SubCategory?
@State private var selectedDate = Date()
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(entity: Language.entity(), sortDescriptors: []) var languages: FetchedResults<Language>
@FetchRequest(entity: SubCategory.entity(), sortDescriptors: []) var subCategories: FetchedResults<SubCategory>
var body: some View {
NavigationView {
ZStack {
Form {
Section("Learning Schedule") {
Picker("Please choose a language", selection: $selectedLanguage) {
ForEach(languages, id: \.self) {
Text($0.name ?? "Unknown")
}
}
Text("You selected: \(selectedLanguage?.name ?? "Unknown")")
}
}
}
}
}
}
I can tap on the form row, and be presented with a list of languages from the CoreData 'languages' entity. But when I tap on a language in the picker in the view that was navigated to, it doesn't place a checkmark against it and return to the form screen with my choice. It just says "Unknown" in the "You selected:" Text Field.
Language and SubCategory Entities:
Picker
might be having a hard time matching theid
and the selection parameter. Do the entities have explicit IDs that you can use instead of.self
? And can you add them to theText
with.id()
? – AlvinobjectID
so you could tag your objects usingtag($0.objectID)
but from my tests this doesn't help. – Domingo