Oleg Gordiichuk Oleg Gordiichuk - 12 days ago 6
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

Something like this?

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

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