json - Cannot display value in UITableView -


i getting error saying "nsstring not have member 'subscript'" on following line.

cell.textlabel?.text = objname[indexpath.row] 

here code (error line @ bottom).

 func makegetrequest(){          var url : string = "http://apiairline.yome.vn/api/get-airport"         var request : nsmutableurlrequest = nsmutableurlrequest ()         request.url = nsurl(string: url)         request.httpmethod = "get"         request.setvalue("1454310830", forhttpheaderfield: "timestamp")         request.setvalue("f8675bfb33f792eeef665da66848623539978204b3b1b036c79aa38218dd7061", forhttpheaderfield: "hash")          nsurlconnection.sendasynchronousrequest(request, queue: nsoperationqueue(), completionhandler:{ (response:nsurlresponse!, data: nsdata!, error: nserror!) -> void in             var error: autoreleasingunsafemutablepointer<nserror?> = nil             let jsonresult: nsdictionary! = nsjsonserialization.jsonobjectwithdata(data, options:nsjsonreadingoptions.mutablecontainers, error: error) as? nsdictionary              var _names: nsmutablearray = nsmutablearray()             if (jsonresult != nil) {                 let dataarray = jsonresult["airport_list"] nsarray;                         var _names: nsmutablearray = nsmutablearray()                 item :anyobject in dataarray{                     let obj = item nsdictionary                     let airport_name = obj["airport_name"] nsstring                     let alias = obj["alias"] nsstring                     let code = obj["code"] nsstring                     let iata = obj["iata"] nsstring                     let location = obj["location"] nsstring                     let type = obj["type"] nsstring                      _names.addobject(airport_name)                     _names.addobject(alias)                     _names.addobject(code)                     _names.addobject(iata)                     _names.addobject(location)                     _names.addobject(type)                       self.congchuoi = airport_name + alias + code + iata + location + type                     println(self.congchuoi)                     self.tabledata = _names                                                    }                                                  } else {                 println("failed")             }          })     }   override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {         let cell = tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) uitableviewcell           var airport_list = tabledata[indexpath.row].valueforkey("airport_list") nsdictionary        var objname = airport_list.valueforkey("airport_name") nsstring        cell.textlabel?.text = objname[indexpath.row]            return cell     } 

there few problems code. first sendasynchronousrequest runs in background, , has not finished once tableview displayed. if didn't have compilation error, wouldn't work. need call reloaddata within completion handler...

func makegetrequest(){      var url : string = "http://apiairline.yome.vn/api/get-airport"     var request : nsmutableurlrequest = nsmutableurlrequest ()     request.url = nsurl(string: url)     request.httpmethod = "get"     request.setvalue("1454310830", forhttpheaderfield: "timestamp")     request.setvalue("f8675bfb33f792eeef665da66848623539978204b3b1b036c79aa38218dd7061", forhttpheaderfield: "hash")      nsurlconnection.sendasynchronousrequest(request, queue: nsoperationqueue(), completionhandler:{ (response:nsurlresponse!, data: nsdata!, error: nserror!) -> void in         var error: autoreleasingunsafemutablepointer<nserror?> = nil         let jsonresult: nsdictionary! = nsjsonserialization.jsonobjectwithdata(data, options:nsjsonreadingoptions.mutablecontainers, error: error) as? nsdictionary          if (jsonresult != nil) {             dataarray = jsonresult["airport_list"]              _names = [nsdictionary]()             item: anyobject in dataarray {                 let obj = item nsdictionary                 _names.addobject(item)             }             self.tabledata = _names             dispatch_async(dispatch_get_main_queue()) {                 self.tableview.reloaddata()             }                                          } else {             println("failed")         }      }) } 

and then:

func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {        let cell = tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) uitableviewcell        let airport = self.tabledata[indexpath.row]        let airport_name = airport["airport_name"] string        cell.textlabel?.text = airport_name    } 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -