【发布时间】:2020-01-08 12:04:00
【问题描述】:
我正在尝试显示过滤后的 List,其中仅包含与 self.day[index] 完全匹配的 subjects。但是,当我尝试为此使用if 子句时,我收到错误Unable to infer complex closure return type; add explicit type to disambiguate。有人可以找到任何其他方法将subject 过滤为subject.day 以等于self.days[index] 吗?这是我的代码,谢谢:
import SwiftUI
import CoreData
struct ProfileView: View {
@Environment(\.managedObjectContext) var moc: NSManagedObjectContext
@FetchRequest(
entity: Subject.entity(),
sortDescriptors:[
//NSSortDescriptor(keyPath: \Subject.day, ascending: true),
NSSortDescriptor(keyPath: \Subject.start, ascending: true)
]
) var subjects: FetchedResults<Subject>
@State private var showAddScreen = false
@State private var name: String = ""
@State private var surname: String = ""
let days = ["Pondelok", "Utorok", "Streda", "Štvrtok", "Piatok"]
var body: some View {
Form {
ForEach(0 ..< days.count) { index in
Section {
Text(self.days[index])
List {
ForEach(self.subjects, id: \.self) { subject in //here the { shows an error, If I remove the if clause, it works, but obviously I don't have subjects filltered, which is what I need
if subject.day == self.days[index] {
SubjectCell(name: "\(subject.name!)",
place: "\(subject.place!)",
start: self.formatTime(date: subject.start ?? Date()),
end: self.formatTime(date: subject.end ?? Date()),
type: subject.type,
occurance: subject.occurance)
}
}
.onDelete(perform: self.deleteSubject)
【问题讨论】: