dimanche 8 février 2015

Swift if statement not working. Clearly a bug, but whats the pattern

I have the following code fragment in an app. The 'if' statement wasn't properly evaluated. Even though score = -100 and bestScore = 200, the if statement if score < bestScore does not run the code within it. bestScore stays at 200.


If I add a print statement within the if, it suddenly starts working. I tried it in a playground without the print statement and it works fine. I've replace some expressions with 'true' for simplicity.



var bestScore = 200
var score = 200
if true {
if true {
score = -100
}
}
if score < bestScore {
print("Testing") // When this wasn't here it wasn't working
bestScore = score
bestMove = move
}


I've seen this sort of strange behavior in Swift before, rarely, but I can't figure out a pattern to explain when this happens. This is with Xcode 6.1.1 (6A2008a).


Aucun commentaire:

Enregistrer un commentaire