I'm still beginner in iOS development. I'm using Swift 3, Alamofire 4 and SwiftyJson for my project. I try to fetch the data from JSON. JSON return in single array. Here is the return :
JSON :
[{
"id": "1",
"post_title": "Event_1",
"post_content": "Taylor Swift",
"start_date": "2016-11-23",
"end_date": "2016-11-23",
"post_date": "2016-11-18"
}, {
"id": "2",
"post_title": "Event_2",
"post_content": "Nickelback",
"start_date": "2016-11-15",
"end_date": "2016-11-16",
"post_date": "2016-11-15"
}, {
"id": "3",
"post_title": "Event_3",
"post_content": "Nirvana",
"start_date": "10.00pm on 22-02-2012",
"end_date": "6.00am on 23-02-2012",
"post_date": "2016-07-10"
}]
Swift :
var newsArray : [[String:Any]] = []
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return newsArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomTableViewCell
let currentNews = newsArray[indexPath.row]
if let post_content = currentNews["post_content"] {
cell.lbl_content.text = post_content as? String
}
return cell
}
func getJSON(){
Alamofire.request(BASE_URL).responseJSON {
response in
switch response.result {
case .success(let value):
let json = JSON(value)
print(json)
if let tempArray = json as? [[String: Any]] {
self.newsArray = tempArray
self.tableView.reloadData()
}
case .failure(let error):
print(error)
}
}
}
Issue :
Looks like tempArray don't hold any value.
I try to implement the value in tableViewCell.
Any help is appreciated and many thanks.