How to split an Int to its individual digits?

后端 未结 8 1285
误落风尘
误落风尘 2020-12-13 06:18

I am trying to split an Int into its individual digits, e.g. 3489 to 3 4 8 9, and then I want to put the digits in an Int array.

I have already tried putting the num

8条回答
  •  青春惊慌失措
    2020-12-13 07:05

    You can try this:

    var number = "123456"
    var array = number.utf8.map{Int($0)-48}
    

    You can make use of the utf8 property of String to directly access the ASCII value of the characters in the String representation of your number.

提交回复
热议问题