Xcode 11 ... can't even loop

The following code is from the Stanford Swift lecture that Apple endorses:


@IBOutlet var cardButtons: [UIButton]!


@IBAction func touchCard(_ sender: UIButton) {

flipCount += 1

if let cardNumber = cardButtons.firstIndex(of: sender)

{

game.chooseCard(at: cardNumber)

updateViewFromModel()

}

}


func updateViewFromModel()

{

for index in cardButtons.indices {

var button = cardButtons[index]

var card = game.cards[index]

//Loop always iterates here <-- the problem

if card.isFaceUp

{

//button.setTitle(emoji( for: card), for: .normal)

button.backgroundColor = card.isMatched ? .lightGray : .orange

button.isEnabled = card.isMatched ? false : true

} else

{

//button.setTitle("", for: .normal)

button.backgroundColor = .orange

}

print("Apple should be ashamed of xcode 11")

}

}


The code never reaches the if-else statements. It exits the loop after the var card statement (using let produced the same result). It took me hours to identify this. This ran in xcode 10.

You should format the code with code formatter tool <>. That makes reading and referencing much easier.



@IBOutlet var cardButtons: [UIButton]!

@IBAction func touchCard(_ sender: UIButton) {
        flipCount += 1
     
        if let cardNumber = cardButtons.firstIndex(of: sender)
        {
            game.chooseCard(at: cardNumber)
            updateViewFromModel()
        }
    }

func updateViewFromModel()
    {
        for index in cardButtons.indices {
          
            var button = cardButtons[index]
            var card = game.cards[index]
            //Loop always iterates here <-- the problem
            if card.isFaceUp
            {
                //button.setTitle(emoji( for: card), for: .normal)
                button.backgroundColor = card.isMatched ? .lightGray : .orange
                button.isEnabled = card.isMatched ? false : true
            } else
            {
                //button.setTitle("", for: .normal)
                button.backgroundColor = .orange
            }
            print("Apple should be ashamed of xcode 11")
        }
    }



Do you get your print about XCode 11 on console ?

I understand no.


So, a common practice for debugging is add print at selected locations.


Line 14, add:

print("indices", cardButtons, cardButtons.indices)


to check if you enter in the func and if your array is empty.

Of course, you have checked that you did connect the buttons to the IBOutletCollection ?

Hi Claude31. Thanks for responding. I've been messing with this for hours and hours. What I know for certain is this code worked in xcode10. i think I'm really over trying to make it work and quickly getting over IOS development. I suspect that there may be something in OS 10.15 that will correct this.

Xcode 11 ... can't even loop
 
 
Q