ios - How would I be able to ensure json elements are not nil? -


i've tried use optional binding in code still comes nil when execute , debug wondering if have used incorrect method?

here code shows me trying parse json

here's code use try , parse json:

import foundation  protocol listingmodelprotocol: class {     func itemsdownloaded(items: nsarray) }   class listingmodel: nsobject, nsurlsessiondatadelegate {      weak var delegate: listingmodelprotocol!      var data : nsmutabledata = nsmutabledata()      let urlpath: string = "http://booksmart.hol.es/service.php" // changed path service.php lives       func downloaditems() {          let url: nsurl = nsurl(string: urlpath)!         var session: nsurlsession!         let configuration = nsurlsessionconfiguration.defaultsessionconfiguration()           session = nsurlsession(configuration: configuration, delegate: self, delegatequeue: nil)          let task = session.datataskwithurl(url)          task.resume()      }      func urlsession(session: nsurlsession, datatask: nsurlsessiondatatask, didreceivedata data: nsdata) {         self.data.appenddata(data);      }      func urlsession(session: nsurlsession, task: nsurlsessiontask, didcompletewitherror error: nserror?) {         if error != nil {             print("failed download data")         }else {             print("data downloaded")             self.parsejson()         }        }     func parsejson() {          var jsonresult: nsmutablearray = nsmutablearray()          do{             jsonresult = try nsjsonserialization.jsonobjectwithdata(self.data, options:nsjsonreadingoptions.allowfragments) as! nsmutablearray          } catch let error nserror {             print(error)          }          var jsonelement: nsdictionary = nsdictionary()         let properties: nsmutablearray = nsmutablearray()          for(var = 0; < jsonresult.count; i+=1)         {              jsonelement = jsonresult[i] as! nsdictionary              let property = propertymodel()              //the following insures none of jsonelement values nil through optional binding             if let propertytype = jsonelement["property type"] as? string,                 let price = jsonelement["price"] as? string,                 let distance = jsonelement["distance"] as? string              {                  property.propertytype = propertytype                 property.price = price                 property.distance = distance               }              properties.addobject(property)          }          dispatch_async(dispatch_get_main_queue(), { () -> void in              self.delegate.itemsdownloaded(properties)          })     } } 

here's code use download data database:

import foundation  class propertymodel: nsobject {      //properties      var propertytype: string?     var price: string?     var distance: string?       //empty constructor      override init()     {      }      //construct @propertytype, @price , @distance parameters      init(propertytype: string, price: string, distance: string) {          self.propertytype = propertytype         self.price = price         self.distance = distance      }       //prints object's current state      override var description: string {         return "property type: \(propertytype), price: \(price), distance: \(distance)"      }   } 

and here's code use try , table cells in swift:

import uikit  class first_resultsviewcontroller: uiviewcontroller, uitableviewdatasource, uitableviewdelegate,    listingmodelprotocol  {      //properties      var feeditems: nsarray = nsarray()     var selectedproperties : propertymodel = propertymodel()     @iboutlet weak var propertylisttableview: uitableview!      override func viewdidload() {         super.viewdidload()         //set delegates , initialize homemodel          self.propertylisttableview.delegate = self         self.propertylisttableview.datasource = self          let listingmodel = listingmodel()         listingmodel.delegate = self         listingmodel.downloaditems()          // additional setup after loading view.     }       func itemsdownloaded(items: nsarray) {          feeditems = items         self.propertylisttableview.reloaddata()     }      func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int {         // return number of feed items         return feeditems.count      }      func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {         // retrieve cell         let cellidentifier: string = "basiccell"         let mycell: uitableviewcell = tableview.dequeuereusablecellwithidentifier(cellidentifier)!         // location shown         let item: propertymodel = feeditems[indexpath.row] as! propertymodel         // references labels of cell         mycell.textlabel!.text = item.propertytype          return mycell     }       /*     // mark: - navigation      // in storyboard-based application, want little preparation before navigation     override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) {         // new view controller using segue.destinationviewcontroller.         // pass selected object new view controller.     }     */   } 

the following code post , comment, not correct.

//the following insures none of jsonelement values nil through optional binding if let propertytype = jsonelement["property type"] as? string,     let price = jsonelement["price"] as? string,     let distance = jsonelement["distance"] as? string 

the above not ensuring values not nil. if statement checking if of jsonelement not nil enter , set properties.

also, if of above properties not string in ur json response, not enter if statement. should checking type return. replace as? double type json response return.

if let propertytype = jsonelement["property type"] as? string,     let price = jsonelement["price"] as? double,     let distance = jsonelement["distance"] as? double {      property.propertytype = propertytype     property.price = "\(price)"     property.distance = "\(distance)"  } 

if want set empty string when nil, should using as string ?? "".

//the following ensure when element nil, change empty string , update our attributes  let propertytype = jsonelement["property type"] as? string ?? ""  let price = jsonelement["price"] as? string ?? ""  let distance = jsonelement["distance"] as? string? ??  property.propertytype = propertytype property.price = price property.distance = distance 

you no longer need if statement anymore not nil.


Comments