局勢: 我有一個通過SwiftyJSON解析的JSON響應。我正在用數據填充表格單元格。一切都很酷,但現在我需要從我的JSON中刪除一些字典,並且它不會讓我以任何方式(循環或其他條件)來執行此操作。收到JSON的如何過濾XCODE中的JSON響應?
例子:
{"post-comments" : [
{
"post" : "new",
"_id" : "1",
},
{
"post" : "new",
"_id" : "2",
},
{
"post" : "post with title",
"_id" : "23",
},
{
"post" : "new",
"_id" : "29",
},
{
"post" : "post with title",
"_id" : "90",
},
{
"post" : "post with title",
"_id" : "33",
}
]
}
我試着擺脫一些詞典,可以說如果「後」 ===「新」 - 我需要將它們刪除從我的JSON,並繼續填充左數據我的單元格。
完全卡住..任何想法將不勝感激。
下面是完整的tableviewcontroller:
class TableViewContr: UITableViewController {
let Comments : String = "http://localhost:3000/api/comments/"
var json : JSON = JSON.null
override func viewDidLoad() {
super.viewDidLoad()
getPostComments(Comments)
}
func getPostComments(getcomments : String) {
Alamofire.request(.GET, getcomments).responseJSON {
response in
guard let data = response.result.value else {
return
}
self.json = JSON(data)
self.tableView.reloadData()
}
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch self.json["post-comments"].type {
case Type.Array:
return self.json["post-comments"].count
default:
return 1
}
}
func populateFields(cell: TableViewContrCell, index: Int) {
guard let comment = self.json["post-comments"][index]["post"].string else {
return
}
cell.commentContent!.text = comment
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewContrCell
populateFields(cell, index: indexPath.row)
return cell
}
}
如何ř填充細胞PLZ添加一些代碼數據源陣列。 – Nishant
將JSON字典解析爲自定義對象(類/結構)。不要傳遞字典。字典是低層次的,並且與它們一起工作很困難並且代碼是不可讀的。 – Sulthan
@Nishant更新 –