iOS programming, fetching JSON data -


i have added project swiftyjson.swift file , trying data web. project runs until line trying array json in dictionary. cannot understand problem is, guessing has stupid in beginning learning swift.

i trying print in console name of movies url , after manage achieve performance, try summary of movie , put them in tableview.

import uikit  class firstviewcontroller: uiviewcontroller {      override func viewdidload() {         super.viewdidload()          //grab status code , check if transfer successful == 200         let requesturl: nsurl = nsurl(string: "https://itunes.apple.com/us/rss/topmovies/limit=50/json")!         let urlrequest: nsmutableurlrequest = nsmutableurlrequest(url: requesturl)         let session = nsurlsession.sharedsession()         let task = session.datataskwithrequest(urlrequest) {             (data, response, error) -> void in              let httpresponse = response as! nshttpurlresponse             let statuscode = httpresponse.statuscode              if (statuscode == 200) {                  //sort through stations key , cast data array of dictionaries                 do{                     let json = try nsjsonserialization.jsonobjectwithdata(data!, options:.allowfragments)                     print("bbbbb")  // here on, doesn't print anymore                     if let movies = json["entry"] as? [[string: anyobject]] {                         print(movies)                         print("test")                          movie in movies {                              if let name = movie["name"] as? string {                                 print("mmmm")                                 print("%@ (built %@)",name)                              }                         }                      }                  }catch {                     print("error json: \(error)")                 }             }         }     task.resume() 

entry json array, use .array

if let movies = json["entry"].array {   movie in movies {      // stuff   } } 

also general tip. not cast values e.g.

movie["something"] as? string 

rather use built in features:

movie["something"].string 

update

looking closer on code see acctually not using swiftyjson.swift @ all. use swifty parse json text , json object:

let jsonobj = json(data: yourdata) // data nsdata 

please have @ documentation:

https://github.com/swiftyjson/swiftyjson

i think reading section "why typical json handling in swift not good?". section explains native , "bad" way of managing json in swift, real documentation further down.


Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -