xcode - Swift NSURLCONNECTION fatal error while parsing json -


i trying access api using following code. getting error "fatal error: unexpectedly found nil while unwrapping optional value" when trying parse json.

i not sure why error happening. data not nil.

var urlfull = nsurl(string: url)!     var urlrequest = nsurlrequest(url: urlfull)     let queue:nsoperationqueue = nsoperationqueue()      nsurlconnection.sendasynchronousrequest(urlrequest, queue: queue, completionhandler: {         (response, data, error) -> void in         println(response)         println(data)         println(error)          if let anerror = error {             println(error)         } else {             var jsonerror: nserror? = nil             let post = nsjsonserialization.jsonobjectwithdata(data, options: nil, error: &jsonerror) nsdictionary             if let ajsonerror = jsonerror {                 println("error parsing")             } else {                 println("the post is: " + post.description)             }           }     }) 

the problem forced cast: as nsdictionary. whatever's being returned can't casted nsdictionary.

you should use optional casting (as?) , optional unwrapping (if let…) when parsing json:

let post = nsjsonserialization.jsonobjectwithdata(data, options: nil, error: &jsonerror) as? nsdictionary  if let post = post {     // worked! parse post } else {     // it's not dictionary.     println(post)     // see have , debug there } 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -