ios - 如何使用 Swift 修复 iOS 应用程序上的 fatal error ?

标签 ios swift sprite-kit game-center xcode7

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

文件 GameViewController.swift 中存在错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

代码:

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

enter image description here

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

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

最佳答案

正如我对您的建议 previous question , 首先使变量名称不那么模糊。

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

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

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

 let scoreManager = PointsLabel(num: 0)

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

saveHighscore(scoreManager.score)

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

scoreManager.increment()

等等

关于ios - 如何使用 Swift 修复 iOS 应用程序上的 fatal error ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32296973/

相关文章:

swift - 'UIPastebaord.general.hasStrings' 不适用于 iOS 14.0

ios - CoreGraphics 绘制结果到 CGContextDrawPath : invalid context 0x0

ios - 通过 iOS 应用程序将照片和标记的 friend 发布到 Facebook

ios - 如何将 POST 数据(包括 JSON)从 iOS 应用程序发送到网络服务器

ios - iPad 的单独启动图像?

ios - iOS应用程式当机: “Selector name found in current argument registers: appendDictionary:into:”

ios - 如何将高分保存并打印到下一个场景?

ios - 设置循环通知某天

swift - 在不再次运行的情况下更改 touch 内部的 node.name?

swift - 如何使 Sprite 在对象内反弹?