For the last three days I have been trying to make some progress with this, but no matter what I try I can't seem to wrap my head around how to solve this problem. This answer is what got me (I think) almost all the way there (but not quite): Capturing data from Alamofire
What I am trying to do is get the number of reviews for an app. Because Alamofire does its network calls asynchronously, I am trying to create a callback that will then allow me to actually work with the JSON that it returns. In the code below I am trying to get the reviews JSON outside of my Alamofire function and assign it to the reviewJSON variable so that I can do things with it. What am I doing wrong?
import UIKit
import SwiftyJSON
import Alamofire
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let appStoreReviewsURL: String = "https://itunes.apple.com/de/rss/customerreviews/id=529479190/json"
func getDataFromInterwebs(theURL: String, complete:(reviews: JSON) -> ()) {
Alamofire.request(.GET, theURL).responseJSON { response in
guard response.result.error == nil else {
print("error calling GET ")
print(response.result.error!)
return
}
if let value = response.result.value {
let appReviewsFromAppStore = JSON(value)
complete(reviews: appReviewsFromAppStore)
}
else {
print("error parsing")
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
var reviewJson = getDataFromInterwebs(appStoreReviewsURL){ completion in
return completion}
print(reviewJson)
}