SwiftUI错误:无法推断复杂的闭包返回类型;添加显式类型以消除歧义

时间:2020-01-05 08:28:43

标签: ios swift swiftui

我知道以前曾问过与该错误有关的问题,但我检查了一下,他们还没有解决我的问题。

我有一个名为 data 的字典,其中包含 event 对象。我正在将rowData与数据中的所有事件进行比较,以查看它们是否匹配。搜索完成后,我不想创建或更新视图,我只想更新数据字段。

struct addToMyMoves {
    var rowData: row
    //rowData is passed into the struct from an outside View

    func addToMoves() {
        ForEach(data){ event in
            //Error: Unable to infer complex closure return type; add explicit type to disambiguate

                if (event.name == rowData.name)  {
                        event.favorite.toggle()


            }

        }

    }
}

当我尝试将if语句放入类似的帖子中建议的Group {}内时,如下所示,我收到“无法推断出通用参数'Content'”错误

struct addToMyMoves {
    var rowData: row
    //rowData is passed into the struct from an outside View

    func addToMoves() {
        ForEach(data){ event in
            //Error: Unable to infer complex closure return type; add explicit type to disambiguate
            Group {
                if (event.name == rowData.name)  {
                        event.favorite.toggle()

                }
            }

        }

    }
}

1 个答案:

答案 0 :(得分:1)

ForEach不是新的迭代语句,它是序列数据上的SwiftUI视图构建器。在您的情况下,您需要使用过时的for语句:

   for event in data {
            if (event.name == rowData.name)  {
                    event.favorite.toggle()
        }
    }