Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
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?
value
[Foo]
It simply like this
let sum = array.reduce(0) { $0 + $1.value }
Add a comment
The same way as with plain numbers:
let foos: [Foo] = ... let sum = foos.lazy.map { $0.value }.reduce(0, +)
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.