string - Unwrapping Json Swift (found nil) -
i trying decode json in xcode, not succed 1 of them.
this :
[ { "id": "1", "title": "bmw", "price": "500.00", "description": "330", "addeddate": "2015-05-18 00:00:00", "user_id": "1", "user_name": "canovas", "user_zipcode": "32767", "category_id": "1", "category_label": "vehicules", "subcategory_id": "2", "subcategory_label": "motos", "bdd": {} } "pictures": [ { "name": "http://cars.axlegeeks.com/sites/default/files/4315/media/images/2014_bmw_z4_sdrive28i_3790993.jpg" } ] } ]
and want value of "name" "pictures" lines have error "unexpectedly found nil while unwrapping value".
for others values proceed way :
let jsondata:nsdictionary = nsjsonserialization.jsonobjectwithdata(urldata!, options:nsjsonreadingoptions.mutablecontainers , error: &error) as! nsdictionary //browse json datas let resp = jsondata["0"] as! nsdictionary let user_namer = resp["user_name"] as! string let user_zipcoder = resp["user_zipcode"] as! string let titler = resp["title"] as! string let pricer = resp["price"] as! string let descriptionr = resp["description"] as! string
thank's !
pictures in not in subdictionary other values are. should work, should check values if nil before force cast them.
if let picturearray = jsondata["pictures"] as? nsarray { if let picturedict = picturearray.firstobject as? nsdictionary { if let picturename = picturedict.objectforkey("name") as? string { nslog("picture name: %@", picturename) } } }
jsondata contains 2 sub-dictionaries, 1 without key , 1 key 'pictures'. pictures array containing 1 sub-dictionary.
Comments
Post a Comment