Umitk Umitk - 1 year ago 125
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, "").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 {
} }

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 Source

You can try like this

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

Hope this will fix your problem

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download