简体   繁体   中英

Unable to infer complex closure return type SwiftUI ForEach

I'm trying to show a filtered List which contains just subjects that match the exact self.day[index] . However, when I try to use the if clause for this, I get the error Unable to infer complex closure return type; add explicit type to disambiguate Unable to infer complex closure return type; add explicit type to disambiguate . Can somebody find out any other way to filter subject by subject.day to be equal to self.days[index] please? Here's my code, thank you:

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)

将整个if {..}包装成Group {..}解决了问题

Would not it be better to filter out subjects based on the same criteria, then return a list of them. There are opportunities to do it in FetchRequest or inside List {} , then use return ForEach(filteredSubjects, id: \\.self) {... .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM