简体   繁体   中英

How convert String to [[Int]]

var str =  "[[1,2,3], [4,0,7], [5,6,8]]"
var digits: [[Int]]

How to convert str in digits in Swift?

String looks like a json. Try json parsing.

let str = "[[1,2,3], [4,0,7], [5,6,8]]"
if let digits = (try? JSONSerialization.jsonObject(with: Data(str.utf8))) as? [[Int]] {
    print(digits) //[[1, 2, 3], [4, 0, 7], [5, 6, 8]]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM