问题描述
Xcode在ForEach行显示报错信息:
Non-constant range: argument must be an integer literal
报错代码为:
var total = rows * columns
ForEach(0..<total) { index in }
经排查发现报错的原因为ForEach的范围参数必须是常量和确定的值。
total是一个计算变量,只有在运算时才能确定,因此编译器无法在编译时推断这个范围,因此产生该报错。
解决方案为,在ForEach中使用 id:\.self 明确标识符:
ForEach(0..<total,id: \.self) { index in }
