本文介绍了函数不按顺序运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在viewdidload我有以下方法:

In viewdidload i have below methods:

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

我希望按顺序运行方法,但打印语句实际上是先调用。

I expect to run methods in order but print statement is actually getting called first.

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
}

我认为运行函数的解决方案会将上面的函数做成一个闭包,但我不知道我该怎么做。 / p>

I think the solution to run functions in order would be making above function into a closure but I'm not sure how I can do it.

推荐答案

您无法从异步任务返回。

You can not return from an asynchronous task.

您的Alamofire任务在后台执行,您正在返回默认值,这就是为什么它看起来像被跳过 -

Your Alamofire task is executed in the background, and you are returning a default value, that is why it looks like it is skipped - but it's just launched in the background and the result is ignored.

解决方案是使用完成处理程序(回调)而不是返回。

The solution is to use a "completion handler" (a callback) instead of a return.

示例:

// (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)
                    }
                }
            }
    }
}

你这样称呼,并且有一个尾随闭包:

You call it like that, with a "trailing closure":

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

这篇关于函数不按顺序运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 21:28
查看更多