将JSON数组附加到字符串数组

时间:2017-06-16 17:53:52

标签: ios arrays json swift swifty-json

我想将JSON数组转换为String数组,然后将字符串数组追加到字符串数组中。我正在处理的JSON数组如下:

let allPosts = JSON(value)

//using SwiftyJSON to do something else

// [["0","1","2","3"],["username1","username12","username123","username1234"]]

到目前为止我尝试过的(我使用的是SwiftyJSON):

let postsIndec =  allPosts[0].arrayValue

//also tried: self.indec.append(contentsOf: postsIndec) //Xcode tells me to remove "contentsOf: "

self.indec.append(postsIndec) // indec being: var indec = [String]()

在最后一行中,我得到了错误Cannot convert value of type '[JSON]' to expected argument type 'String'这有意义,但明确地将其转换为字符串

由于子数组中的字符串数量可能大于或小于4(此处用作示例),因此无法循环预定义次数。你能帮帮我吗?我找不到任何描述同一问题的问题。

1 个答案:

答案 0 :(得分:1)

尝试使用map将[JSON]转换为[String]:

self.indec.append(contentsOf: postsIndec.map {$0.stringValue} )