Tom*_*you 6 ios swift swifty-json
我有一个swiftyJSON对象,例如:
[{
"location" : "http://...",
"img" : "http://...",
"commentCount" : 0,
"timestamp" : 1432460217550,
}]
Run Code Online (Sandbox Code Playgroud)
我希望能够将另一个swiftyJSON对象附加到它,使它看起来像:
[{
"location" : "http://...",
"img" : "http://...",
"commentCount" : 0,
"timestamp" : 1432460217550,
},
{
"location" : "http://...",
"img" : "http://...",
"commentCount" : 1,
"timestamp" : 1432460217571,
}
]
Run Code Online (Sandbox Code Playgroud)
我不能在swiftyJSON对象上使用+=或.append.我怎样才能做到这一点?
如你所说,swiftyJSON没有附加功能.
你可以做的是将swiftyJSON对象解析为anyObject类型的数组并追加它们.
let json = JSON(data: data!)
var JSONObject = JSON(json["content"].arrayObject! + json["content"].arrayObject!)
Run Code Online (Sandbox Code Playgroud)
数据 - >从HTTP请求接收的NSData对象.
extension JSON {
mutating func merge(other: JSON) {
for (key, subJson) in other {
self[key] = subJson
}
}
func merged(other: JSON) -> JSON {
var merged = self
merged.merge(other: other)
return merged
}
}
Run Code Online (Sandbox Code Playgroud)
维克多的回答对我不起作用。但我通过将 JSON 对象放入data一个数组中解决了这个问题,如下所示:
var data: [JSON] = []
Run Code Online (Sandbox Code Playgroud)
并使用以下代码:
self.data = self.data + JSON["content"].arrayValue
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5305 次 |
| 最近记录: |