Setting a Default Property Value with a Closure or Function

I'm having difficulty with lines 5 - 11. Line 5 and 6 have a for-in within a for-in. I haven't encountered a situation like that before. How might I reason about this loop and it's contents? Is there an applicable concept within the manual I may reference? Is this called recursio

struct Chessboard {
     let boardColors: [Bool]()
          var temporaryBoard = [Bool]()
          var isBlack = false
          for i in 1...8 {
               for j in 1...8 {
                    temporaryBoard.append(isBlack)
                    isBlack = !isBlack
               }
               isBlack = !isBlack
          }
          return temporaryBoard
     }()
  

Your post doesn't show any code. Can you update it, please?

This is only to create a chessboard, with alternating black and white cells. No recursion at all here!


temporaryBoard will simply be an array of 64 true or false: [false, true, false, true …]


            var isBlack = false          // Start with a white cell
            for i in 1...8 {      // The 8 lines
               // When on one line, explore all cells of the line (the 8 columns)
               for j in 1...8 {
                    temporaryBoard.append(isBlack)     // Add the cell to the array
                    isBlack = !isBlack                    // next cell in the line is of alternating color
               }
               isBlack = !isBlack     // At the end of line, one need to start the new line with a different color than the end of previous line (see how a chessboard is drawn)
          }

what do i and j represent?

just indexes ; if you want it clearer, you can call them iRow and jColumn


            var isBlack = false          // Start with a white cell
            for iRow in 1...8 {      // The 8 lines
               // When on one line, explore all cells of the line (the 8 columns)
               for jColumn in 1...8 {
                    temporaryBoard.append(isBlack)     // Add the cell to the array
                    isBlack = !isBlack                    // next cell in the line is of alternating color
               }
               isBlack = !isBlack     // At the end of line, one need to start the new line with a different color than the end of previous line (see how a chessboard is drawn)
          }


But in fact, you dont even need them, because they are not used in the loop, and could write:

            for _ in 1...8 {      // The 8 lines
               // When on one line, explore all cells of the line (the 8 columns)
               for _ in 1...8 {


"_" represents a dummy var ; it is not the same for the 2 loops, but a different internal one created by the compiler that you don't even see.

How does the computer know to create row two after the first 8 columns are added? Also, why is forced unwrapping needed as it applies to isBlack?

Accepted Answer

It is not forced unwrapping.

!isBlack means the negation of present isBlack value (The "Not" operator)


In fact, this does not creates lines, it just creates 64 cells in a array, with alternating colors.


Look:


for iRow in 1...8 {

You start here with iRow = 1

Then you loop for all jColumn, from 1 to 8 (8 times)

for jColumn in 1...8 {

When you reach jColum = 8, you end the jColumn loop

And then, you iterate on iRow, which is now set to 2

And restart jColumn from 1.

thank you very much, that helped a great deal.

Setting a Default Property Value with a Closure or Function
 
 
Q