Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Cookies for whoever can help

I am trying to get to the total weight lost from all of my clients. I have written the following code. I can get the sum, but I would have to manually append the array each time an instance of Client is created. How do restructure this code to be more efficient, and append the array automatically?

class Client {

var name: String
var startWeight: Double
var currentWeight: Double
var listOfWeightLoss: [Double] = []

var weightLoss: Double {
    get { return startWeight - currentWeight }

}

init(name: String, startWeight: Double, currentWeight: Double) {
    self.name = name
    self.startWeight = startWeight
    self.currentWeight = currentWeight




        listOfClients.append(self)
    }

}

var listOfClients: [Client] = []

let juanJimenez = Client.init(name: "Juan Jimenez", startWeight: 173.8, currentWeight: 156.6)

let jacieJimenez = Client.init(name: "Jacie Jimenez", startWeight: 175.0, currentWeight: 131.0)

var totalWeightLoss = [jacieJimenez.weightLoss, juanJimenez.weightLoss]

var sum = totalWeightLoss.reduce(0, +)

print("(sum)")