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

Store Variable inside Completion handler IOS Swift

我试图将本地玩家分数存储在 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)")

我猜你的请求是异步的,这意味着你无法知道它何时完成。所以你试图在异步任务之前调用的主线程中调用打印函数。尝试在 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)")
    }
}

谢谢我已经解决了问题,如果我在完成后调用打印。

 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)")
 }

我模拟了一些代码,想提出一些(可能是架构/风格)问题。请参阅代码。下面的解决方案在 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()