Take a look at this code:
struct Person { var name: String var age: Int } var array = [Person(name: "John", age: 10), Person(name: "Apple", age: 20), Person(name: "Seed", age: 30)]
I am trying to change the value of a structure property inside a loop.
Of course, I can change Person to class , and it works fine. However, I do not understand why item is le .
Oh, I just figured it out, for item in... just created the copied actual object inside the array , this means that it is automatically declared by the let constant.
So why can't I change the value of its property.
My question is: besides changing Person to class , how to change Person properties inside a loop?
Edit:
Thanks @Zoff Dino with the original answers for index in 0..<array.count .
However, this is just a simplified question.
I want to archive using higher order functions with an array of structures, for example:
array.each { item in ... }.map { item in ... }.sort { } ...
source share