Chapter 14/Page 632 - Building reusable views

Hello Team,

the code example shown on page 632 gives me the following error:

Cannot convert value of type ‘ClosedRange’ to expected argument type ‘Range’

The problem lies in the ForEach loop using the wrong type to iterate over the values.

ForEach(0...numberRows) { row in HStack

Instead using:

ForEach(0...numberRows-1, id:\.self){ row in HStack

or

For Each(0..<numberRows){row in HStack

keeps XCode silent. However as you mentioned, on page later it doesn’t give the expected result.

Thanks for pointing this out. This is a bit of a subtle change between versions of SwiftUI. The simplest solution is to adjust the line as you did in your second example and change the calculation of numberRows to reflect the last item no longer being included in the range.

The next edition of the book will update to fix this issue.

Just ran into the same problem. You can use

For Each(0…<numberRows+1) { row in HStack

to get the expected result.

@qm13 Thank you for sharing your solution - much appreciated!