ios - 无法推断复杂的闭包返回类型 SwiftUI ForEach

标签 ios swift foreach swiftui

我试图显示一个过滤后的List,其中仅包含与self.day[index]完全匹配的subjects。但是,当我尝试为此使用 if 子句时,收到错误 Unable to infer Complexclosure return type;添加显式类型以消除歧义。有人可以找到任何其他方法来通过 subject.day 过滤 subject 使其等于 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)

最佳答案

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

关于ios - 无法推断复杂的闭包返回类型 SwiftUI ForEach,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59645479/

相关文章:

javascript - Discord.JS - 使用角色将 Discord 中的用户移动到您的 VC

android - iOS/Android 回到 OpenGL 1.1 而不是使用 2.0

ios - 将 12 小时日期格式快速转换为 24 小时格式,反之亦然?

ios - exc_bad_instruction exc_i386_invop on UIButton title change

ios - 从方法签名中获取参数类型(镜像/反射/自省(introspection)?)

java - 如果数组列表中有多种数据类型,如何使用java中的每个循环?

php - 我想从这个 JSON URL 回显 IMDB ID、标题、质量和嵌入 URL

ios - 点击时 UIButton 图像不会改变

iOS:如何突出显示带有蓝色模糊光环的按钮?

ios - 如何正确计算 UIScrollView 的最小缩放因子?