I have an array with custom objects. Now what I need to do is when ever an element will be added or removed I need a method to be called where I'll compute totalCost. Is it possible??
class Company {
var name: String?
....
....
var employees[Employee] = []
var totalCost: Float = 0
}
class Employee {
var name: String?
.....
.....
var salary: Float = 0
}
I have an option to override getter method of totalCost
where I need go through all the employee object to calculate totalCost. But I don't want to do this cause I might have a huge number of employees and on every call this iteration can be a expensive operation.
Any suggestion will be appreciable.