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

Popular posts from this blog

Load Balancing in Bluemix using custom domain and DNS SRV records -

oracle - pls-00402 alias required in select list of cursor to avoid duplicate column names -

python - Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>] error -