简体   繁体   中英

Alamofire - save data in stored properties

I have a model class which contain some user data. The data was download from server in JSON format. I would like to parse the data and save into array.

Alamofire.request(.GET, requestUrl).responseJSON(completionHandler: {
            response in

            let data: JSON = JSON(response.2.value!)
            self.jsonData = data
}

but the problem is that the jsonData is unavailable outside of this closure. How can I do this? I spend few hours on this problem and I do not have idea :(.

Try to pass your own closure to request and call it in completion handler:

makeRequest(success: { (data: JSON) in self.jsonData = data })
func makeRequest(#success: (JSON) -> ()) {
  Alamofire.request(.GET, requestUrl).responseJSON(completionHandler: {
        response in

        let data: JSON = JSON(response.2.value!)
        success(data)
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM