将array.description字符串转换为Array

时间:2017-02-22 20:38:14

标签: arrays swift

有没有简单的方法将类似的字符串转换为:[[1,2],[1,3],[1,4]]到带数组的数组?

我很高兴。

2 个答案:

答案 0 :(得分:1)

最简单的选择是将其转换为JSON并返回:

System.out.println(" Is Manager object available : " + (mgr == null ? "is null" : "not null"));

答案 1 :(得分:0)

我为它制作了这个方法并且它有效:

func convertToArray(string: String) -> [[Int]] {
    var arrayOrder = 0
    var mainArray: [[Int]] = []
    var array: [Int] = []

    for character in string.characters {
        if character == "[" {
            arrayOrder += 1
        }

        if arrayOrder >= 2 && character != "[" && character != "]" && character != "," {
            let integer: Int = Int(String(character))!
            array.append(integer)
        }

        if character == "]" {
            if arrayOrder >= 2 {
                mainArray.append(array)
                array = []
            }

            arrayOrder -= 1
        }
    }

    return mainArray
}