【问题标题】:Unable to infer complex closure return type SwiftUI ForEach无法推断复杂的闭包返回类型 SwiftUI ForEach
【发布时间】: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)

【问题讨论】:

    标签: ios swift foreach swiftui


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      根据相同的标准过滤掉主题,然后返回它们的列表不是更好吗?有机会在FetchRequestList {} 内部进行,然后使用return ForEach(filteredSubjects, id: \.self) {...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多