Oleg Gordiichuk Oleg Gordiichuk - 8 months ago 52
Swift Question

How to get all digits of the Int value

Is it possible to get all digits of the

Int
variable in the Swift not converting this variable to the
string
?

Small example how i am doing this now:

let number = 123456

let array = String(number).characters.map{Int(String($0)) ?? 0}

Answer Source

Something like this?

var number = 123456
var array = [Int]()

while number > 0 {
    array.append(number % 10)
    number = number / 10
}
array.reverse()