首页 > 解决方案 > 无法转换“ClosedRange”类型的值'到预期的参数类型'范围'

问题描述

我有这个代码:

VStack {
    ForEach(0...2) { i in
        HStack {
            ForEach(0...2) { j in
                Text("test")
            }
        }
    }
}

这给了我Cannot convert value of type 'ClosedRange<Int>' to expected argument type 'Range<Int>'两个 ForEach 语句的错误

我已经看到有关此错误的线程,并且对范围事物的工作原理有所了解,但实际上并非如此。我想知道如何解决这个错误。

标签: swifttype-conversionswiftui

解决方案


可以使用ClosedRange但使用不同的ForEach构造函数,提供id,如

    VStack {
        ForEach(0...2, id: \.self) { i in
            HStack {
                ForEach(0...2, id: \.self) { j in
                    Text("test")
                }
            }
        }
    }

推荐阅读