ios - Not able to hit the endpoint when making a GET request in swift -


hello trying make request server failing hit endpoints checked logs , im not hitting final endpoint. endpoint needs authorization has been added request field. attempting json values , display them table cell. when attempt retrieve values return nil. never attempts hit endpoint. attempted put print statement print("session)" check if calls datawithrequest if fails so. please doing wrong here?

 func getlocaldata() -> [[string:anyobject]] {         var json:[[string:anyobject]]?         let endpoint = "http://******************"          let url:nsurl? = nsurl(string: endpoint)         let session = nsurlsession.sharedsession()          let request:nsmutableurlrequest = nsmutableurlrequest(url: url!)          request.httpmethod = "get"         request.setvalue("application/json", forhttpheaderfield: "content-type")         request.addvalue(token!, forhttpheaderfield: "authorization-header")           let task = session.datataskwithrequest(request) {(data, response, error) -> void in             print("session") //not able in here             guard let data = data error == nil else {                 //network error                 print("error network: \(error?.localizeddescription)")                 return             }              let httpresponse = response as! nshttpurlresponse//httpurlresponse             let statuscode = httpresponse.statuscode              if statuscode == 200 {                 {                      print("here buddy")                      json = try nsjsonserialization.jsonobjectwithdata(data, options: .allowfragments) as? [[string: anyobject]]                    // return json                 }                  catch {                   print("error in json")                 }             }         }          task.resume()          print("json: \(json)") //prints nil never attempts make request server          return json  } 

and in tablelistcontroller attempt call function , complains fatal error: unexpectedly found nil while unwrapping optional value

class deallistcontroller: uitableviewcontroller {     let mydeals = deallist()     var dealsarray = []      override func awakefromnib() {         super.awakefromnib()          dealsarray = mydeals.getlocaldata()! //complains error here    } } 

let add these things info.plist enter image description here

update: let use completion handler instead of returning value

func getlocaldata(completion: (json: [[string:anyobject]]?) -> void) {     let endpoint = "http://******************"      let url:nsurl? = nsurl(string: endpoint)     let session = nsurlsession.sharedsession()      let request:nsmutableurlrequest = nsmutableurlrequest(url: url!)      request.httpmethod = "get"     request.setvalue("application/json", forhttpheaderfield: "content-type")     request.addvalue("", forhttpheaderfield: "authorization-header")       let task = session.datataskwithrequest(request) {(data, response, error) -> void in         print("session") //not able in here         guard let data = data error == nil else {             //network error             print("error network: \(error?.localizeddescription)")             completion(json: nil)             return         }          let httpresponse = response as! nshttpurlresponse//httpurlresponse         let statuscode = httpresponse.statuscode          if statuscode == 200 {             {                 print("here buddy")                 let json = try nsjsonserialization.jsonobjectwithdata(data, options: .allowfragments) as? [[string: anyobject]]                 // return json                 completion(json: json)             }              catch {                 print("error in json")                 completion(json: nil)             }         }     }      task.resume() } 

and:

mydeals.getlocaldata { json in     print("json: \(json)")     dealsarray = json! } 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -