无法访问 属性
Accessing property is not possible
我想在 MainView
中显示一个单独的 SubjectView
。
无法访问 SubjectView
中 subject
上的 属性 topics
。
我该怎么办?谢谢。
更新:
ForEach(subject.topics, id: \.self) { topic in
Text(topic.title)
}
...可能是有问题的部分。
文件:SubjectView
import SwiftUI
struct SubjectView: View {
@State var subject: Subject
var body: some View {
List {
ForEach(subject.topics, id: \.self) { topic in
Text(topic.title)
}
}
}
}
文件MainView
import SwiftUI
struct MainView: View {
@Environment(\.managedObjectContext) var managedObjectContext
@FetchRequest(entity: Subject.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Subject.name, ascending: true)]) var subjects: FetchedResults<Subject>
@State var subjectName: String = ""
var body: some View {
NavigationView {
Form {
List {
ForEach(subjects, id: \.self) { subject in
NavigationLink(
destination: SubjectView(subject: subject),
label: {
Text(subject.name ?? "-")
})
}
}
}
.navigationTitle("Schulfächer")
}
}
}
Screenshot of the Subject entity
SubjectView
处的错误:var body: some View
读取:
Failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project
ForEach
的集合类型必须符合RandomAccessCollection
。 topics
不是因为它是 (NS)Set
.
一个简单的解决方案是 sort
将 Set
变成 Array
。或者显式创建一个数组,该行假定 topics
是原生的 Swift Set<Topic>
Array(subject.topics)
注意:不要随意使用\.self
。确定一个唯一的属性and/or采用Identifiable
。例如,您可以使用唯一的 NSManagedObject
实例的 NSManagedObjectID
。
@State var subject: Subject
在SubjectView
中应该是
@ObservedObject var subject: Subject
https://developer.apple.com/documentation/swiftui/managing-model-data-in-your-app
@StateObject
用于初始化ObservableObject
s
关于你的话题
ForEach((subject.topics.allObjects as? [Topic]) ?? [], id: \.self) { topic in
应该可以
我想在 MainView
中显示一个单独的 SubjectView
。
无法访问 SubjectView
中 subject
上的 属性 topics
。
我该怎么办?谢谢。
更新:
ForEach(subject.topics, id: \.self) { topic in
Text(topic.title)
}
...可能是有问题的部分。
文件:SubjectView
import SwiftUI
struct SubjectView: View {
@State var subject: Subject
var body: some View {
List {
ForEach(subject.topics, id: \.self) { topic in
Text(topic.title)
}
}
}
}
文件MainView
import SwiftUI
struct MainView: View {
@Environment(\.managedObjectContext) var managedObjectContext
@FetchRequest(entity: Subject.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Subject.name, ascending: true)]) var subjects: FetchedResults<Subject>
@State var subjectName: String = ""
var body: some View {
NavigationView {
Form {
List {
ForEach(subjects, id: \.self) { subject in
NavigationLink(
destination: SubjectView(subject: subject),
label: {
Text(subject.name ?? "-")
})
}
}
}
.navigationTitle("Schulfächer")
}
}
}
Screenshot of the Subject entity
SubjectView
处的错误:var body: some View
读取:
Failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project
ForEach
的集合类型必须符合RandomAccessCollection
。 topics
不是因为它是 (NS)Set
.
一个简单的解决方案是 sort
将 Set
变成 Array
。或者显式创建一个数组,该行假定 topics
是原生的 Swift Set<Topic>
Array(subject.topics)
注意:不要随意使用\.self
。确定一个唯一的属性and/or采用Identifiable
。例如,您可以使用唯一的 NSManagedObject
实例的 NSManagedObjectID
。
@State var subject: Subject
在SubjectView
中应该是
@ObservedObject var subject: Subject
https://developer.apple.com/documentation/swiftui/managing-model-data-in-your-app
@StateObject
用于初始化ObservableObject
s
关于你的话题
ForEach((subject.topics.allObjects as? [Topic]) ?? [], id: \.self) { topic in
应该可以