arrays - 在 swift 中将字符串转换为数组(来自 Parse 服务器)

标签 arrays swift casting

<分区>

我正在使用包含整数数组 ([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/

相关文章:

arrays - Excel - 数组公式跳过空单元格

arrays - 子数组长度 > 0 的最小和

arrays - 使用 Int 对数组进行排序

ios - NSURLConnection sendSynchronousReques 替换

ios - 不使用键盘调用 func textfield(...)

将多个结构转换为 char 字符串

javascript - 如何对数组中的多个元素重新排序?

c# - LINQ 实体结果到 List<string>

c - "(type)variable"和 "*((type *)&variable)"之间有什么区别(如果有)?

c++ - 如何在类中声明和初始化字符串数组?