ios - Nested JSON access / assigning values to UITextField -


i making small app allows users catalogue books looking isbn on openlibrary. idea isbn search takes returned json data , uses populate uitextfields user can further edit before saving.

i'm having issue accessing nested json data , putting uitextfield. accusing top level of dictionary , putting uitextfields works fine, when try go deeper, error:

cannot assign value of type string? value of type 'string!'.

the json data it's taking here: https://openlibrary.org/api/books?bibkeys=0586057242&f&jscmd=data&format=json

below code:

import uikit  class itemviewcontroller: uiviewcontroller {      @iboutlet weak var titlefield: uitextfield!     @iboutlet weak var authorfield: uitextfield!      var session: nsurlsession!     var items = nsobject()      var lookupid = "0586057242" // passed in elsewhere.       required init (coder adecoder: nscoder) {         super.init(coder: adecoder)          let config = nsurlsessionconfiguration.defaultsessionconfiguration()         session = nsurlsession(configuration: config, delegate: nil, delegatequeue: nil)          fetchitem()     }      func fetchitem() {         let requeststring = ("https://openlibrary.org/api/books?bibkeys=" + lookupid + "&f&jscmd=data&format=json")         if let url = nsurl(string: requeststring) {             let req = nsurlrequest(url: url)              let datatask = session.datataskwithrequest(req) {                 (data, response, error) in                 if data != nil {                     var error: nserror?                     if let jsonobject = nsjsonserialization.jsonobjectwithdata(data, options: nsjsonreadingoptions.mutablecontainers, error: &error) as? nsdictionary  {                          if let itemdictionary: anyobject  = jsonobject["\(self.lookupid)"]{                             if let id = itemdictionary as? nsobject {                                 self.items = id                                  println("\(self.items)")                                  // works.                                 self.titlefield.text = itemdictionary["title"] as? string                                  // not work.                                 self.authorfield.text = itemdictionary["authors"]["name"] as? string                               }                          }                     } else {                         if let error = error {                             println("error parsing json: \(error)")                         }                     }                 } else {                     println("error fetching item: \(error.localizeddescription)")                 }             }             datatask.resume()         }     }  } 

i don't @ understand why doesn't work, doing wrong way i'm accessing values?

you might try this:

var names = [string]() if let authors = itemdictionary["authors"] as? nsarray {     author in authors {         if let author = author as? nsdictionary,             let name = author["name"] as? string {                 names.append(name)         }     } } self.authorfield.text = join(",", names) 

hope helps


Comments