<分区>
我正在使用包含整数数组 ([1,4,5,6,7]) 的列对我的解析服务器进行查询,我想在变量中使用此结果作为数组。我应该以什么方式转换以确保发生这种情况?
as! [Int]
不适合我。
<分区>
我正在使用包含整数数组 ([1,4,5,6,7]) 的列对我的解析服务器进行查询,我想在变量中使用此结果作为数组。我应该以什么方式转换以确保发生这种情况?
as! [Int]
不适合我。
最佳答案
我想到的第一件事是:
给定这个字符串:
var str = "[1,2,3,4,5,6]"
str = str.replacingOccurrences(of: "[", with: "")
str = str.replacingOccurrences(of: "]", with: "")
str = str.replacingOccurrences(of: " ", with: "") //This if you have spaces between comas and numbers
let array = str.components(separatedBy: ",").flatMap({ Int($0)})
你会得到一个整数数组
希望对你有帮助
关于arrays - 在 swift 中将字符串转换为数组(来自 Parse 服务器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48182694/