Reputation: 1050
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.
Upvotes: 1
Views: 2813
Reputation: 201
Currently I'm not using Swift 3, Alamofire 4. I have found some solution in the below link. Please check, if you can any solution from this. Link: http://ashishkakkad.com/2015/10/how-to-use-alamofire-and-swiftyjson-with-swift/
Alamofire.request("http://api.androidhive.info/contacts/").responseJSON { (responseData) -> Void in
if((responseData.result.value) != nil) {
let swiftyJsonVar = JSON(responseData.result.value!)
if let resData = swiftyJsonVar["contacts"].arrayObject {
self.arrRes = resData as! [[String:AnyObject]]
}
if self.arrRes.count > 0 {
self.tblJSON.reloadData()
}
}
}
Upvotes: 0
Reputation: 4402
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? NSArray {
self.newsArray = tempArray
print(tempArray)
self.tableView.reloadData()
}
case .failure(let error):
print(error)
}
}
}
Try above line of code. May help you.
Upvotes: 0
Reputation: 1319
You can try this code.
Alamofire.request(BASE_URL).responseJSON {
response in
switch response.result {
case .success(let value):
//to get JSON return value
if let result = response.result.value {
let JSON = result as! [[String: Any]]
//then use guard to get the value your want
}
case .failure(let error):
print(error)
}
}
Upvotes: 1
Reputation: 13577
Please try Below
if let tempArray = json as? [[String: Any]] {
self.newsArray = tempArray.mutableCopy()
self.tableView.reloadData()
}
Upvotes: 0
Reputation: 3310
Try this
if let tempArray = json as? [[String: Any]] {
self.newsArray = tempArray
self.tableView.reloadData()
}
Use json
only don't json[]
Upvotes: 0