I currently have an array in a Swift class that is of type Bool, declared as follows:
public var cardIsTaken: [Bool]
For purposes of keeping up with a Swift style guide that calls for avoiding indexed for loops when possible, I have something like this:
for takenFlag in cardIsTaken {
takenFlag = true
}
.. which gives me the error message "cannot assign to 'let' value 'takenFlag'"
Out of curiosity, I tried declaring it with "var", as in:
for var takenFlag in cardIsTaken {
takenFlag = true
}
.. which just gives me a whole slew of different, unrelated error messages.
I am 99% sure it means at this time, I cannot use "for foo in array" to iterate through an array if I want to change each value, but if there IS a way to do it, I'd be all ears.
The best way to do this is to use the array's built in mapping function.
cardIsTaken = cardIsTaken.map { isTaken in true }
来源:https://stackoverflow.com/questions/28863292/in-swift-can-i-use-a-for-in-enumeration-to-initialize-or-reset-an-array