Break A Number Up To An Array of Individual Digits

后端 未结 5 1938
陌清茗
陌清茗 2020-12-02 00:32

If I have the integer 123 and I want to break the digits into an array [1,2,3] what is the best way of doing this? I have messed around with this a lot and I have the follo

5条回答
  •  广开言路
    2020-12-02 01:02

    I'd say if it isn't broke don't fix it. I can thing of one other way, but it's not any shorter or anything:

    var number = 123
    var digits = map(String(number)) { String($0).toInt() ?? 0 }
    

提交回复
热议问题