2016-09-15 4 views
0

Ich verwende Alamofire mit SwiftyJSON. Ich kann erfolgreich von API wie in unten lesen:Int (NSCFNumber) aus dem Array des Wörterbuchs lesen

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() 
} 
} } 

Aber ich kann nicht die Werte dict [ "id"] und dict [ "userId"] aus Wörterbuch in der Zelle angezeigt bekommen.

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

Und das ist die declarition meiner Array of Wörterbuch an der Spitze:

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

Sie für jede Hilfe sehr viel Dank.

Antwort

1

Sie können wie versuchen, diese

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

hoffen, dass dies Ihr Problem

1

Dies ist das Beispielwörterbuch beheben, erhalten wir aus server.Here die die ID eine ID und Benutzer-ID ist sind integers.So Geben Sie anstelle von "casting" in "string" die Zeichenfolge "int" oder "NSNumber" ein.

{ 
"userId": 1, 
"id": 2, 
"title": "qui est esse", 
"body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla" 

}

cell.label_id.text = String(dict["id"] as? Int ?? 0) 
cell.label_userId.text = Stringdict["userId"] as? Int ?? 0) 

ODER

cell.label_id.text = String(dict["id"] as? NSNumber ?? 0) 
cell.label_userId.text = String(dict["userId"] as? NSNumber ?? 0) 
0

Wenn ich richtig Ihre Json Antwort verstanden, können Sie es als unten:

if let id = dict["id"] { 
     cell.label_id.text = "\(id)" 
    } 
    if let userID = dict["userId"] { 
     cell.label_userId.text = "\(userID)" 
    } 
+0

Vielen Dank ‚Santosh ". Deine Antwort ist wahr wie Kuntal, aber ich akzeptierte seine, wie ich es vorher gesehen habe. – Umitk

Verwandte Themen