Reputation: 6723
I have following model
class Foo {
var value: Double
var color: UIColor
init?(value: Double, color: UIColor) {
self.value = value
self.color = color
}
}
How can I sum all value
property inside of [Foo]
using reduce?
Upvotes: 14
Views: 8117
Reputation: 130102
The same way as with plain numbers:
let foos: [Foo] = ...
let sum = foos.lazy.map { $0.value }.reduce(0, +)
Upvotes: 9