【问题标题】:SwiftUI Error: Unable to infer complex closure return type; add explicit type to disambiguateSwiftUI 错误:无法推断复杂的闭包返回类型;添加显式类型以消除歧义
【发布时间】:2020-01-05 08:28:43
【问题描述】:

我知道之前有人问过涉及此错误的问题,但我检查了它们,但它们并没有解决我的问题。

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

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 {} 中时,如下所示,我收到“无法推断通用参数'内容'”错误

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()

                }
            }

        }

    }
}

【问题讨论】:

    标签: ios swift swiftui


    【解决方案1】:

    ForEach 不是新的迭代语句,它是序列数据的 SwiftUI 视图构建器。在您的情况下,您需要旧的for 声明:

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

    【讨论】:

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