update: Xcode 10.1 • Swift 4.2.1 or later
let strings = ["1.9","2.7","3.1","4.5","5.0"]
let doubles = strings.compactMap(Double.init)
let sum = doubles.reduce(0, +)
print(sum) // 17.2
If you dont need the intermediary collection
let sum = strings.reduce(0) { $0 + (Double($1) ?? .zero) }
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments