函数没有按顺序运行

在viewdidload我有下面的方法:

var savedSummonerID : Int? savedSummonerID = LeagueMethodHelper.retrieveSummonerIDFromSummonerName(serverNameAbbreviation, summonerName : summonerName) print("haha \(self.savedSummonerID)") 

我期望按顺序运行方法,但print statement实际上首先被调用。

retrieveSummonerIDFromSummonerName如下所述:

 static func retrieveSummonerIDFromSummonerName(serverName : String, summonerName : String) -> Int { var savedSummonerID = 0 Alamofire.request(.GET, "https://\(serverName).api.pvp.net/api/lol/\(serverName)/v1.4/summoner/by-name/\(summonerName)?api_key=(key)") .responseJSON { response in print(response.response?.statusCode) // URL response if let JSON = response.result.value { if let summonerJSONInfo = JSON[summonerName.lowercaseString] as? [String:AnyObject] { if let summonerID = summonerJSONInfo["id"] as? Int { savedSummonerID = summonerID print(summonerID) } if let SummonerName = summonerJSONInfo["name"] as? String { print(SummonerName) } } } } return savedSummonerID } 

我认为按顺序运行函数的解决scheme将使上面的函数成为闭包,但我不知道我怎么能做到这一点。

您不能从asynchronous任务返回。

你的Alamofire任务是在后台执行的,而你正在返回一个默认值,这就是为什么它看起来像被跳过 – 但它只是在后台启动,结果被忽略。

解决scheme是使用“完成处理程序”(callback)而不是回报。

例:

 // (id: Int)->() is the completion handler signature that we add to your method parameters static func retrieveSummonerIDFromSummonerName(serverName : String, summonerName : String, completion:(id: Int)->()) { Alamofire.request(.GET, "https://\(serverName).api.pvp.net/api/lol/\(serverName)/v1.4/summoner/by-name/\(summonerName)?api_key=xxx") .responseJSON { response in print(response.response?.statusCode) // URL response if let JSON = response.result.value { if let summonerJSONInfo = JSON[summonerName.lowercaseString] as? [String:AnyObject] { if let summonerID = summonerJSONInfo["id"] as? Int { // use the completion where the result becomes available completion(id: summonerID) } if let SummonerName = summonerJSONInfo["name"] as? String { print(SummonerName) } } } } } 

你这样称呼,用一个“尾随的closures”:

 LeagueMethodHelper.retrieveSummonerIDFromSummonerName(serverNameAbbreviation, summonerName: summonerName) { (id) in savedSummonerID = id print(savedSummonerID) } 

您正在从retrieveSummonerIDFromSummonerName开始一个asynchronous任务。 当它完成时,该块被执行:

  .responseJSON { response in print(response.response?.statusCode) // URL response if let JSON = response.result.value { if let summonerJSONInfo = JSON[summonerName.lowercaseString] as? [String:AnyObject] { if let summonerID = summonerJSONInfo["id"] as? Int { savedSummonerID = summonerID print(summonerID) } if let SummonerName = summonerJSONInfo["name"] as? String { print(SummonerName) } } } } 

如果你想在后面执行print语句,你可以把它包含在这个块的末尾,像这样:

  .responseJSON { response in print(response.response?.statusCode) // URL response if let JSON = response.result.value { if let summonerJSONInfo = JSON[summonerName.lowercaseString] as? [String:AnyObject] { if let summonerID = summonerJSONInfo["id"] as? Int { savedSummonerID = summonerID print(summonerID) } if let SummonerName = summonerJSONInfo["name"] as? String { print(SummonerName) } } } print("haha \(self.savedSummonerID)") } 

或者,您可以实现某种callback,您可以在完成块的末尾以相同的方式调用callback。