将 JSON 数组附加到字符串数组
Appending JSON array to string array
我想将 JSON 数组转换为字符串数组,然后将字符串数组附加到字符串数组。我正在处理的 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(此处用作示例)。你能帮我吗?我找不到任何描述相同问题的问题。
尝试使用映射将 [JSON] 转换为 [String]:
self.indec.append(contentsOf: postsIndec.map {[=10=].stringValue} )
我想将 JSON 数组转换为字符串数组,然后将字符串数组附加到字符串数组。我正在处理的 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(此处用作示例)。你能帮我吗?我找不到任何描述相同问题的问题。
尝试使用映射将 [JSON] 转换为 [String]:
self.indec.append(contentsOf: postsIndec.map {[=10=].stringValue} )