ios - Store completion value to an outside variable -
as title suggest. want store/put result of api call via datatask in model or dictionary can access in viewdidload()
class viewcontroller: uiviewcontroller, cllocationmanagerdelegate { var dict = [[string:any]]() let manager = cllocationmanager() override func viewdidload() { if(cllocationmanager.locationservicesenabled()) { switch(cllocationmanager.authorizationstatus()){ case .notdetermined, .restricted, .denied: print ("") case .authorizedalways, .authorizedwheninuse: manager.delegate = self manager.desiredaccuracy = kcllocationaccuracybest manager.requestwheninuseauthorization() manager.requestlocation() // locationmanager function } } } func locationmanager(_ manager: cllocationmanager, didupdatelocations locations: [cllocation]) { var lat:string = "" var long:string = "" if let location = locations.first{ lat = string(location.coordinate.latitude) long = string(location.coordinate.longitude) getapi(lat: lat, long: long, completion: { data in self.dict = data // tested , returns data }) } } func getapi(lat:string,long:string, completion: @escaping ([[string:any]]) -> void){ //my datatask code here } }
the variable has value within locationmanager function due self. unable pass value in variable outside closure block
how can access variable data in getapi closure globally? or pass down variable can accessed in locationmanager
Comments
Post a Comment