I am not able to modify my model class variable even using mutating func
keyword in a method?
So basically I have wrapped my problem in a very easy way I have a class Car
that has 3 variable id
, start
, and modelNo
After that initialize an empty Car model array and then I want to show 10 cars, creating a range for loop which iterates 1...10
, initializing the Car model class and appending it to original cars array.
There is a check for first 5 cars id will be 0 and last 5 cars id will be 1
I want a filter in which same id's last car will start so I have created a method filtered and modifying the start variable but not able to modify it. Can you please help me what I am doing wrong?
Please see my complete code and copy paste it to the playground.
struct Car {
var id = 0
var start = false
var modelNo: String
init(start: Bool, id: Int, model: String) {
self.start = start
self.id = id
self.modelNo = model
}
mutating func startCar(status: Bool) {
start = status
}
}
var arrCars:[Car] = [Car]()
for i in 1...10 {
let md = Car(start: false, id: i <= 5 ? 0 : 1, model: "Model\(i)")
arrCars.append(md)
}
private func filtered() {
for item in arrCars {
let filteredItems = arrCars.filter { $0.id == item.id }
if filteredItems.count > 0 {
if var lastItem = filteredItems.last {
print("Will start \(lastItem.modelNo)")
lastItem.startCar(status: true)
}
}
}
let cars = arrCars.filter { (car) -> Bool in
car.start == true
}
print(cars.count)
}
filtered()
Any help will be appreciated.