Swift: Reduce function -
i have array
let digits = ["3", "1", "4", "1"]
i want convert them int
value 3141
i have tried
let digits = ["3", "1", "4", "1"] let mydigit = int(digits.reduce("") { $0 + $1 }) print(mydigit) //3141
but don't want wrap int()
of digits.reduce("") { $0 + $1 }
, want use reduce
achieve this. (that can use int()
inside reduce
function)
how this? , there ways else achieve this?
any helps appreciated. thanks.
another approach reduce. there no way around converting strings have ints @ point.
let digits = ["3", "1", "4", "1"] let number = digits.reduce(0) { 10 * $0 + int($1, radix: 10)! } print(number)
Comments
Post a Comment