I have the following problem:
When I change the value of an array in a for-loop the array is not saved with this new value.
Example:
Code Block swift var numbers = [2, 3, 4, 6, 9] let theNumber = 4 print("Numbers old: \(numbers)") for var number in numbers { if number == theNumber { print("Numbers are identical") number = 10 /*save the changed number in array*/ } } print("Numbers new: \(numbers)")
The console output is:
So the value of the array is not changed.Numbers old: [2, 3, 4, 6, 9]
Numbers are identical
Numbers new: [2, 3, 4, 6, 9]
How to modify the code so that the value of the array is changed?
Is there an easy way to do so?
Help is very welcome!
Thank you!