ios - using Swift to get contents of URL using NSURLSession, however, it return errors -
i've got 3 errors, "'nearbyvc.type' not confirm protocol 'uitableviewdatasource'" @ error1, "'nearbyvc.type' not have member named 'session'" @ error2 , "expected declaration" @ error3. however, cannot figure out why such errors appeared.if know hot solve, please me.
// ※error1※ class nearbyvc: uiviewcontroller,uitableviewdelegate,uitableviewdatasource{ @iboutlet var listview: uitableview? var tabledata = [] override func viewdidload() { super.viewdidload() // additional setup after loading view. } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } func tableview(tableview: uitableview,numberofrowsinsection section: int) -> int{ return 1 } func tableview(tableview: uitableview,cellforrowatindexpath indexpath: nsindexpath) -> uitableview{ let cell:uitableviewcell = uitableviewcell(style: uitableviewcellstyle.subtitle, reuseidentifier: "mytestcell") let rowdata:nsdictionary = self.tabledata[indexpath.row] nsdictionary cell.textlabel.text = "row#\(indexpath.row)" cell.detailtextlabel?.text = "subtitle #\(indexpath.row)" } let url = nsurl(string:"http:~~") let session = nsurlsession.sharedsession() // ※error2※ let task:nsurlsessiontask = session.datataskwithurl(url,completionhandler:apihandler) //use separate handler function in lieu of doing inline clarity func apihandler(data:nsdata!,response:nsurlresponse!,error:nserror!){ if (error != nil) { println("api error:\(error),\(error.userinfo)") } var jsonerror:nserror? var json:nsdictionary = nsjsonserialization.jsonobjectwithdata(data, options: nsjsonreadingoptions.mutablecontainers, error: &jsonerror) nsdictionary if (jsonerror != nil) { println("error parsing json: \(jsonerror)") } else{ let status:string? = json["msg"] as? string println("servar status:\(status)") } } // ※error3※ task.resume() }
error1:
func tableview(tableview: uitableview,cellforrowatindexpath indexpath: nsindexpath) -> uitableview{ let cell:uitableviewcell = uitableviewcell(style: uitableviewcellstyle.subtitle, reuseidentifier: "mytestcell") let rowdata:nsdictionary = self.tabledata[indexpath.row] nsdictionary cell.textlabel.text = "row#\(indexpath.row)" cell.detailtextlabel?.text = "subtitle #\(indexpath.row)" }
i won't analize in body, should return uitableviewcell not uitableview
as specified in protocol:
func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell
error2:
let url = nsurl(string:"http:~~") let session = nsurlsession.sharedsession() // ※error2※ let task:nsurlsessiontask = session.datataskwithurl(url,completionhandler:apihandler)
you cannot initialize constant in class , use initialize one, move function.
error3
fix error2 fix error3.
disclamer: haven't analised whole code you've written , have identified causes of compiler error.
Comments
Post a Comment