ios - Nested JSON data will cause crash using NS Dictionary (in Swift) -



ios - Nested JSON data will cause crash using NS Dictionary (in Swift) -

i'm trying fetch film info in json format using jsonserialization in swift , save nsdictionary. however, calling rotten tomatoes api (in info nested) cause playground crash (without giving me useful errors).

i know code valid, since calling other apis don't nest info works (but don't fulfill need).

here's code:

func getjson(urltorequest: string) -> nsdictionary { var url = nsurl(string: urltorequest) var error: nserror? allow jsondata: nsdata = nsdata.datawithcontentsofurl(url , options: nsdatareadingoptions.datareadingmapped, error: nil) allow jsondict = nsjsonserialization.jsonobjectwithdata(jsondata, options: nsjsonreadingoptions.mutablecontainers, error: &error) nsdictionary homecoming jsondict } allow moviedata: nsdictionary = getjson(apiurl)

here's sample rotten tomatoes api json (shortened illustration purposes, actual json file, not output code)

{ "id": 770672122, "title": "toy story 3", "year": 2010, "genres": ["animation", "kids & family", "science fiction & fantasy", "comedy"], "release_dates": { "theater": "2010-06-18", "dvd": "2010-11-02" }, "ratings": { "critics_rating": "certified fresh", "critics_score": 99, "audience_rating": "upright", "audience_score": 89 } }

this code works me in swift ios app (crashes xcode when run in playground:

func getjson(urltorequest: string) -> nsdictionary { var url = nsurl(string: urltorequest) var error: nserror? allow jsondata: nsdata = nsdata.datawithcontentsofurl(url , options: nsdatareadingoptions.datareadingmapped, error: nil) allow jsondict = nsjsonserialization.jsonobjectwithdata(jsondata, options: nsjsonreadingoptions.mutablecontainers, error: &error) nsdictionary homecoming jsondict } allow apiurl = "http://api.rottentomatoes.com/api/public/v1.0/movies/770672122.json?apikey=9p4xwzpgt8xp2vahnyx2sc6g" allow moviedata: nsdictionary = getjson(apiurl) println(moviedata)

it looks crash getting playground error.

note, swift guaranteed have bugs, beta.

also explicitly state json illustration code, not fro running code.

ios json xcode nsdictionary swift

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -