Umitk Umitk - 2 months ago 8
Swift Question

Reading int(NSCFNumber) from array of dictionary

I am using Alamofire with SwiftyJSON. I can succesfully read from API as in below:

Alamofire.request(.GET, "https://jsonplaceholder.typicode.com/posts").responseJSON { (responseData) -> Void in
if((responseData.result.value) != nil) {
let swiftyJsonVar = JSON(responseData.result.value!)

if let resData = swiftyJsonVar.arrayObject {
self.arrRes = resData as! [[String:AnyObject]]
}
if self.arrRes.count > 0 {
self.results_tableView.reloadData()
}
} }


But i can't get the values dict["id"] and dict["userId"] from dictionary to display in the cell.

var dict = arrRes[indexPath.row]
cell.label_body.text = dict["body"] as? String
cell.label_title.text = dict["title"] as? String
cell.label_id.text = dict["id"] as? String **//prints (nil)**
cell.label_userId.text = dict["userId"] as? String **//prints (nil)**


enter image description here

And this is the declarition of my Array of Dictionary at the top:

var arrRes = [[String:AnyObject]]() //Array of dictionary


Thank you very much for any help.

Answer

You can try like this

if let userId = dict["userId"] {
   cell.label_userId.text = "\(userId)"
}

Hope this will fix your problem