警告内容
ForEachにて、以下のようなコードを書いていたら警告が表示されました。
struct ContentView: View {
let array = ["one", "two", "three", "four"]
var body: some View {
List {
ForEach(0..<array.count) { index in
Text(array[index])
}
}
}
}
Non-constant range: argument must be an integer literal
原因
ForEachの引数にidを渡していないから。
解決方法
ForEachの引数idに、\.selfを渡してあげる
struct ContentView: View {
let array = ["one", "two", "three", "four"]
var body: some View {
List {
ForEach(0..<array.count, id: \.self) { index in
Text(array[index])
}
}
}
}
参考文献

