-1

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

3 回答 3

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)")
    }
}
于 2016-05-29T15:40:46.007 回答
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()
于 2016-05-29T16:04:11.747 回答
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)")
 }
于 2016-05-29T15:56:22.973 回答