Swift-Tips之整數轉陣列

godiscoder發表於2019-03-01

Code

let num = 1234565
extension Int {
    var array: [Int] {
        //swift 4.2
        return String(self).compactMap({Int(String($0))})
        // swift 4.1
        //return String(self).flatMap({Int(String($0))})
    }
}

num.array
//print: [1, 2, 3, 4, 5, 6, 5]
複製程式碼

相關文章