【问题标题】:How to fix a fatal error on iOS Application, using Swift?如何使用 Swift 修复 iOS 应用程序上的致命错误?
【发布时间】:2015-08-30 13:19:52
【问题描述】:

我使用 SpriteKit 和 Xcode 7 beta 和 swift 制作了一个游戏。我尝试添加 GameCenter 和排行榜,但问题是当我运行应用程序(模拟器)时,当我按下排行榜按钮时,模拟器停止运行并且运行时显示错误。我不知道如何解决它。我正在使用 2 个不同的文件:GameViewController.swift 和 PointsLabel.swift

文件 GameViewController.swift 中有错误:

致命错误:在展开可选值时意外发现 nil

代码:

@IBAction func leaderboard(sender: UIButton) {
    saveHighscore(score.score) //<- Xcode say Here is Error but error shows on run
    showLeader()
}

GameViewController.swift:

import GameKit

class GameViewController: UIViewController,UIGestureRecognizerDelegate, GKGameCenterControllerDelegate {

var score: PointsLabel!

override func viewDidLoad() {
    super.viewDidLoad()

    //initiate gamecenter
func authenticateLocalPlayer(){

    let localPlayer = GKLocalPlayer.localPlayer()

    localPlayer.authenticateHandler = {(GameViewController, error) -> Void in

        if (GameViewController != nil) {
            self.presentViewController(GameViewController!, animated: true, completion: nil)
        }

        else {
            print((GKLocalPlayer.localPlayer().authenticated))
        }
     }
  }
}

@IBAction func leaderboard(sender: UIButton) {

    saveHighscore(score.score)

    showLeader()

}



//send high score to leaderboard
func saveHighscore(score:Int) {

    //check if user is signed in
    if GKLocalPlayer.localPlayer().authenticated {

        let scoreReporter = GKScore(leaderboardIdentifier: "Leaderboard_01")

        scoreReporter.value = Int64(score)

        let scoreArray: [GKScore] = [scoreReporter]

        GKScore.reportScores(scoreArray, withCompletionHandler: {error -> Void in
            if error != nil {
                print("error")
            }
        })
    }
}


    //shows leaderboard screen
    func showLeader() {
        let vc = self.view?.window?.rootViewController
        let gc = GKGameCenterViewController()
        gc.gameCenterDelegate = self
        vc?.presentViewController(gc, animated: true, completion: nil)
    }
}

//hides leaderboard screen
func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController)
{
    gameCenterViewController.dismissViewControllerAnimated(true, completion: nil)

}

PointsLabel.swift:

import Foundation
import UIKit
import SpriteKit

class PointsLabel: SKLabelNode {

var score:Int = 0

init(num: Int) {
    super.init()

    fontColor = UIColor.blackColor()
    fontSize = 30.0

    score = num
    text = "\(num)"
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func increment() {
    score++
    text = "\(score)"
}

func setTo(num: Int) {
    self.score = num
    text = "\(self.score)"
 }
}

希望你能帮助我,因为我不知道如何解决它!

【问题讨论】:

    标签: ios swift sprite-kit game-center xcode7


    【解决方案1】:

    正如我为您的previous question 建议的那样,首先要减少变量名称的歧义。

    score 处理程序对象 var score: PointsLabel! 应变为,例如,scoreManager

    下一步:我在您的示例中没有看到此类的任何实例。因此,当您点击按钮时,您要求隐式展开 score 对象及其score 属性,但score 对象不存在。

    实际解决方案取决于您的应用程序逻辑,我无法为您提供魔术代码,但请尝试将 var score: PointsLabel! 替换为:

     let scoreManager = PointsLabel(num: 0)
    

    现在您有了一个有效的分数持有者。然后你可以使用它的score 属性:

    saveHighscore(scoreManager.score)
    

    并使用它的方法来更新分数:

    scoreManager.increment()
    

    等等

    【讨论】:

    • 现在我知道了,谢谢兄弟。我是否需要添加任何其他内容,因为来自应用程序的分数不会添加到排行榜(排行榜得分保持 0)?很抱歉有很多问题,我是初学者!
    • 不客气。但你已经知道我要说什么了:问一个新问题。 :) 这并不严格,但因为在 SO 上,每个问题我们应该只有一个精确的主题。
    • 是的,我会先尝试自己修复它,如果我不能我会问另一个。
    • 我发现了问题,问题是我没有把 scoreManager.increment() 放在正确的位置,我把它放在按钮的功能中,每当我按下排行榜按钮时,分数就会增长 1。你能请编辑问题并告诉我在哪里添加这个“scoreManager.increment()”
    猜你喜欢
    • 1970-01-01
    • 2020-05-06
    • 2020-11-15
    • 2020-12-13
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    相关资源
    最近更新 更多