简体   繁体   English

无法转换“ClosedRange”类型的值<int> '到预期的参数类型'范围<int> '</int></int>

[英]Cannot convert value of type 'ClosedRange<Int>' to expected argument type 'Range<Int>'

I have this code:我有这个代码:

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

This give me error Cannot convert value of type 'ClosedRange<Int>' to expected argument type 'Range<Int>' on both ForEach statements这给了我错误Cannot convert value of type 'ClosedRange<Int>' to expected argument type 'Range<Int>'

I've seen threads on this error and kind of get it a bit on how the range thing works but not really.我已经看到有关此错误的线程,并且对范围事物的工作原理有所了解,但实际上并非如此。 I'm wondering how to fix this error.我想知道如何解决这个错误。

It is possible to use ClosedRange but with different ForEach constructor, providing id , like可以使用ClosedRange但具有不同的ForEach构造函数,提供id ,例如

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

You can use ..< instead of ... for range to be of type Range<Index> instead of ClosedRange<Index>您可以使用..<而不是...使范围的类型为Range<Index>而不是ClosedRange<Index>

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法转换&#39;Range&#39;类型的值 <Int> &#39;到预期的参数类型&#39;Range &lt;_&gt;&#39; - Cannot convert value of type 'Range<Int>' to expected argument type 'Range<_>' 无法覆盖可计数的类型的值封闭范围 <int> 到预期的参数类型Rang <int> - Cannot Cover Value of type countable closedRange<int>to expected argument type Rang <int> 无法将类型“[array]”的值转换为预期的参数类型“Range”<int> '</int> - Cannot convert value of type '[array]' to expected argument type 'Range<Int>' 无法将类型&#39;Int&#39;的值转换为通用类型的预期参数类型&#39;Int&#39; - Cannot convert value of type 'Int' to expected argument type 'Int' in generic 无法将'[Int]'类型的值转换为预期的参数类型'Int' - Cannot convert value of type '[Int]' to expected argument type 'Int' 无法将类型“ NSRange”(也称为“ NSRange”)的值转换为预期的参数类型“ Range” <Index> ”(又称“范围 <Int> “) - Cannot convert value of type 'NSRange' (aka 'NSRange') to expected argument type 'Range<Index>' (aka 'Range<Int>') 无法转换“范围”类型的值<Int> ?&#39; 到预期的参数类型 &#39;Range<String.Index> ? - Cannot convert value of type 'Range<Int>?' to expected argument type 'Range<String.Index>? 无法转换“范围”类型的值<int32> ' 到预期的参数类型 'Range&lt;_&gt;'</int32> - cannot convert value of type 'Range<Int32>' to expected argument type 'Range<_>' 无法将Int类型的值转换为预期的参数类型Bool - Cannot convert value of type Int to expected argument type Bool 错误“无法将类型为“ int”的值转换为预期的参数类型为“ UInt” - Error 'cannot convert value of type 'int' to expected argument type 'UInt'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM