I would create a protocol for your class or structures that contains a value. And change its declaration to non optional.
protocol Valueable {
var value: Int { get }
}
Then you will need to make your class conform to that protocol:
class CustomClass: Valueable {
let value: Int
init(value: Int) {
self.value = value
}
}
Now you can extend the collection protocol with a read only instance property to return the sum of all elements in your array.
extension Collection where Element: Valueable {
var sum: Int {
return reduce(0) { $0 + $1.value }
}
}
let object1 = CustomClass(value: 2)
let object2 = CustomClass(value: 4)
let object3 = CustomClass(value: 8)
let objects = [object1, object2, object3]
let sum = objects.sum // 14
edit/update:
Another option is to extend sequence and add a generic sum method that accepts a key path that its property conforms to AdditiveArithmetic
or add an associated type to the protocol that conforms to AdditiveArithmetic:
protocol Valueable {
associatedtype Value: AdditiveArithmetic
var value: Value { get }
}
extension Collection where Element: Valueable {
var sum: Element.Value { reduce(.zero) { $0 + $1.value } }
}
class CustomClass: Valueable {
let value: Decimal
init(value: Decimal) {
self.value = value
}
}
Usage:
let object1 = CustomClass(value: 123.4567)
let object2 = CustomClass(value: 12.34567)
let object3 = CustomClass(value: 1.234567)
let objects = [object1, object2, object3]
let sum = objects.sum // 137.036937
.mapwould be a better solution) and then just sum the numbers.