为此,请使用闭包,因为它是异步的。示例:
func callTheFunction(){
loadInstallerCount { (count) in
print(count)
}
}
func loadInstallerCount(result:(_:Int) -> Void) {
var count: Int = 0
Alamofire.request(URL, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { response in
if let data = response.result.value{
let jsonData = data as! NSDictionary
if(!(jsonData.value(forKey: "error") as! Bool)) {
count = jsonData.value(forKey: "installationcount") as! Int
}else{
print("couldn't get count")
}
result(count)
}
}
}