Was playing around with the reduce function on collections in Swift.
//Reduce method should return an Int with the value 3141.
let digits = ["3", "1", "4", "1"]
.reduce(0) {
(total:Int, digit:String) in
return ("\(total)" + digit).toInt()!
}
The function is giving the correct output but why does ("0"+"1").toInt()!
return 1 as an Int, rather than 0? The string combined to be turned into an Int is "01"
. I assume this is a String that the function cannot covert to an Int directly. Does it just default to the second character instead?