将变量存储在完成处理程序IOS Swift中

时间:2016-05-29 15:23:13

标签: ios swift variables handler completion

我正在尝试将本地玩家得分存储在pScore中,但是在阻止之后变量总是等于0,我如何将“localPlayerScore!.value”存储到pScore:Int。

    let leaderBoardRequest = GKLeaderboard()
    leaderBoardRequest.identifier = "leaderboard ID"

    leaderBoardRequest.loadScoresWithCompletionHandler {
        (scores, error) -> Void in
        if (error != nil) {
            print("Error: \(error!.localizedDescription)")
        } else if (scores != nil) {
            let localPlayerScore = leaderBoardRequest.localPlayerScore
            self.pScore = Int(localPlayerScore!.value)
        }
    }

    print("Local player's score: \(pScore)")

3 个答案:

答案 0 :(得分:4)

我的猜测是你的请求是异步的,这意味着你无法知道它何时完成。所以你试图在异步任务之前调用的主线程中调用print函数。尝试在self.pScore = Int(localPlayerScore!.value)print("Local player's score: \(pScore)")行中添加断点。你会明白我的意思。无论如何,如果您只是尝试在异步任务中打印数据,您将访问结果,例如:

let leaderBoardRequest = GKLeaderboard()
leaderBoardRequest.identifier = "leaderboard ID"

leaderBoardRequest.loadScoresWithCompletionHandler {
    (scores, error) -> Void in
    if (error != nil) {
        print("Error: \(error!.localizedDescription)")
    } else if (scores != nil) {
        let localPlayerScore = leaderBoardRequest.localPlayerScore
        self.pScore = Int(localPlayerScore!.value)
        print("Local player's score: \(self.pScore)")
    }
}

答案 1 :(得分:0)

谢谢,如果我在完成后调用打印件,我已经对问题进行了排序。

 let leaderBoardRequest = GKLeaderboard()
 leaderBoardRequest.identifier = "leaderboard ID"

 leaderBoardRequest.loadScoresWithCompletionHandler {
     (scores, error) -> Void in
     if (error != nil) {
         print("Error: \(error!.localizedDescription)")
     } else if (scores != nil) {
         let localPlayerScore = leaderBoardRequest.localPlayerScore
         self.pScore = Int(localPlayerScore!.value)
     }
     self.cont()
 }

 func cont() {
     print("Local player's score: \(pScore)")
 }

答案 2 :(得分:0)

我已经嘲笑了一些代码,并想提出一些(可能是架构/风格)问题。看代码。以下解决方案适用于Playground。

import Foundation.NSError

typealias GKLeaderboardCompletion = (Int?,NSError?)->(Void)

class GKLeaderboard {
    var identifier: String = ""
    var localPlayerScore: Int?
    func loadScoresWithCompletionHandler(completion: GKLeaderboardCompletion) {
        sleep(2)
        localPlayerScore = 10
        completion(localPlayerScore, nil)
    }
}

class GKSomeClass {

    var pScore: Int? = nil

    func someFunc() {
        let leaderBoardRequest = GKLeaderboard()
        leaderBoardRequest.identifier = "leaderboard ID"

        // in order to be safe here, better to put "[unowned self]" (read more)
        leaderBoardRequest.loadScoresWithCompletionHandler { [unowned self]
            (scores, error) -> Void in
            if (error != nil) {
                print("Error: \(error!.localizedDescription)")
            } else if (scores != nil) {

                // #1: Why don't you utilize the "scores"?

                // #2: Does "leaderBoardRequest.localPlayerScore == scores" at the time of the callback?

                // #3: What's the type of "leaderBoardRequest.localPlayerScore" here below?
//                self.pScore = Int(localPlayerScore!.value)

                let localPlayerScore = leaderBoardRequest.localPlayerScore
                self.pScore = localPlayerScore

                print("Local player's score: \(self.pScore)")
            }
        }
    }
}

let someClass = GKSomeClass()
someClass.someFunc()