当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - Swift 1.2 -> Swift 2

问题描述:

I'm coding an app for my Kids. I have background in Java but havn't professionally coded in a decade. I don't thoroughly understand how the following line works but it worked in Swift 1.2:

leaderboardRequest.loadScoresWithCompletionHandler({ (scores:[AnyObject]!, error:NSError!) -> Void in

if (error != nil)

{

I need help translating this into working Swift 2.0 code. Could someone please post a translation of working Swift 2.0 code for the following method:

func getHighscores(leaderboardID: String) {

let leaderboardRequest = GKLeaderboard() as GKLeaderboard!

leaderboardRequest.identifier = leaderboardID

if leaderboardRequest != nil

{

leaderboardRequest.loadScoresWithCompletionHandler({ (scores:[AnyObject]!, error:NSError!) -> Void in

if (error != nil)

{

//println("error in alltimeHighscoreForLevel")

print(error.description)

self.updateLocalHighscore()

}

else

{

if(leaderboardRequest != nil){

if(leaderboardRequest.scores != nil ) {

if(leaderboardRequest.scores!.count > 0){

self.updateHighestForLevel(leaderboardRequest)

}

}

}

}

})

}

}

网友答案:

I think this will work for you:

leaderboardRequest.loadScoresWithCompletionHandler({ (scores, error) -> Void in
    if let error = error {
         // from here on your code should work

I think the definition of loadScoresWithCompletionHandler changed from the old code.

网友答案:

Two common ways to solve this:

  • Comment out the line / method which causes the error and start to retype it. The code completion will reveal the proper signature of the method. Then copy and paste the relevant code from the old line / method to the new one and resolve potential errors / warnings.

  • Press ⇧⌘0 and type (or copy&paste) the beginning of the method for example loadScoresWithCompletionHandler into the search field. This will take you to the documentation and you can see the whole declaration including the discussion.

分享给朋友:
您可能感兴趣的文章:
随机阅读: