从表单选择器中选择 CoreData 实体
Choosing CoreData Entities from form picker
我有以下代码:
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([=10=].name ?? "Unknown")
}
}
Text("You selected: \(selectedLanguage?.name ?? "Unknown")")
}
}
}
}
}
}
我可以点击表单行,然后会看到来自 CoreData 'languages' 实体的语言列表。但是,当我在导航到的视图中的选择器中点击一种语言时,它不会根据我的选择在它上面放置一个复选标记,并且 return 到表单屏幕。它只是在“您选择的:”文本字段中显示“未知”。
语言和子类别实体:
正如所写,您没有匹配选择器和 FetchResult 的数组类型。看评论
import SwiftUI
@available(iOS 15.0, *)
struct LearnView: View {
//This is optional Language
@State private var selectedLanguage: Language?
//I commented out variables that are not part of the reproducible body provided
//@State private var selectedCategory: SubCategory?
//@State private var selectedDate = Date()
@Environment(\.managedObjectContext) private var viewContext
//This is FetchedResults<Language>
@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) {
//Map is needed for a plain array of languages
//ForEach(languages.map{[=10=]}, id: \.self) { language in
//This version works too
//The point is to go from FetchedResults<Language> to [Language]
ForEach(Array(languages), id: \.self) { language in
Text(language.name ?? "Unknown")
//The casting is necessary to make it an optional since your @State is an optional
.tag(language as? Language)
}
}
Text("You selected: \(selectedLanguage?.name ?? "Unknown")")
}
}
}
}
}
}
@available(iOS 15.0, *)
struct LearnView_Previews: PreviewProvider {
static var previews: some View {
LearnView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
}
}
我有以下代码:
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([=10=].name ?? "Unknown")
}
}
Text("You selected: \(selectedLanguage?.name ?? "Unknown")")
}
}
}
}
}
}
我可以点击表单行,然后会看到来自 CoreData 'languages' 实体的语言列表。但是,当我在导航到的视图中的选择器中点击一种语言时,它不会根据我的选择在它上面放置一个复选标记,并且 return 到表单屏幕。它只是在“您选择的:”文本字段中显示“未知”。
语言和子类别实体:
正如所写,您没有匹配选择器和 FetchResult 的数组类型。看评论
import SwiftUI
@available(iOS 15.0, *)
struct LearnView: View {
//This is optional Language
@State private var selectedLanguage: Language?
//I commented out variables that are not part of the reproducible body provided
//@State private var selectedCategory: SubCategory?
//@State private var selectedDate = Date()
@Environment(\.managedObjectContext) private var viewContext
//This is FetchedResults<Language>
@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) {
//Map is needed for a plain array of languages
//ForEach(languages.map{[=10=]}, id: \.self) { language in
//This version works too
//The point is to go from FetchedResults<Language> to [Language]
ForEach(Array(languages), id: \.self) { language in
Text(language.name ?? "Unknown")
//The casting is necessary to make it an optional since your @State is an optional
.tag(language as? Language)
}
}
Text("You selected: \(selectedLanguage?.name ?? "Unknown")")
}
}
}
}
}
}
@available(iOS 15.0, *)
struct LearnView_Previews: PreviewProvider {
static var previews: some View {
LearnView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
}
}