Text expression in two nested ForEach loop cannot compile

I want to put text based on the value of 2d array and the index of 2d array. So I tried with ForEach to access array's indices and position text by the current index in 2d array. Here is the code:

var body: some View {
        GeometryReader { geometry in
            let w = geometry.size.width
            let h = geometry.size.height
            ForEach(originNumbers.indices, id: \.self) { i in
                ForEach(originNumbers[i].indices, id: \.self) { j in
                    if originNumbers[i][j] != 0 {
                        Text("\(originNumbers[i][j])").font(.system(size: 500)).minimumScaleFactor(0.001)
                            .lineLimit(1)
                            .frame(height: h / 9.0, alignment: .center)
                            .position(x: j * w / 9.0 + w / 18.0, y: i * h / 9.0 + h / 18.0)
                    }
                }
            }
//            if originNumbers[2][2] != 0 {
//                Text("\(originNumbers[0][0])").font(.system(size: 500)).minimumScaleFactor(0.001)
//                    .lineLimit(1)
//                    .frame(width: w / 9.0, height: h / 9.0, alignment: .center)
//                    .position(x: 2 * w / 9.0 + w / 18.0, y: 2 * h / 9.0 + h / 18.0)
//            }
        }
    }

The compiler complain about it cannot type check the expression in reasonable time. I try to uncomment the comment part and comment the uncomment part in the code block, it works. But when put the same code in two nested ForEach loop, it will not work.

Answered by workingdogintokyo in 676826022

it could be the line with the ".position(...)". Here it is mixing Int and CGFloat. Try this:

.position(x: CGFloat(j) * w / 9.0 + w / 18.0, y: CGFloat(i) * h / 9.0 + h / 18.0)
Accepted Answer

it could be the line with the ".position(...)". Here it is mixing Int and CGFloat. Try this:

.position(x: CGFloat(j) * w / 9.0 + w / 18.0, y: CGFloat(i) * h / 9.0 + h / 18.0)
Text expression in two nested ForEach loop cannot compile
 
 
Q