Reputation: 1
I am interested and beginning in Swift, but I can't fix that :
func countvalue(tableau : [String]){
var b : Int = 0
for var b in tableau {
b++ // Unary operator '++' cannot be applied to an operand of type @lvalue String'
}
print("Il y a \(b) valeurs dans ce tableau.")
}
Upvotes: 0
Views: 819
Reputation: 77641
I think what you want is this...
func countvalue(tableau : [String]){
var b : Int = 0
for _ in tableau {
// the values in the array are not used so just ignore them with _
b++ // Unary operator '++' cannot be applied to an operand of type @lvalue String'
}
print("Il y a \(b) valeurs dans ce tableau.")
}
But the value of b
will be the same if you do...
var b = tableau.count
Except this is a lot more efficient as it does not have to iterate every value of the array.
Upvotes: 2
Reputation: 19855
The b
in your loop is a different variable than the one outside the loop, and is masking it. Since tableau
is an array of String
s, b
in the loop is a String
, and thus cannot be incremented.
Upvotes: 2