Swift SpriteKit:线程1:信号SIGABRT。无法转换SKLabelNode类型的值?

时间:2023-01-23 00:25:53

So I was finishing up a project, and when I ran it and it crashed and I got the Thread 1: signal SIGABRT error and i looked at the description and it gave me this:

所以我正在完成一个项目,当我运行它并且它崩溃了,我得到了线程1:信号SIGABRT错误,我看了描述,它给了我这个:

Could not cast value of type 'SKLabelNode' (0x108ed0b78) to 'Koala_Hop.MCTPointLabel' (0x108091da0). (lldb)

无法将'SKLabelNode'(0x108ed0b78)类型的值转换为'Koala_Hop.MCTPointLabel'(0x108091da0)。 (LLDB)

for this line of code:

对于这行代码:

func loadHighscore() {
    let defaults = NSUserDefaults.standardUserDefaults()
    let highscoreLabel = childNodeWithName("highscoreLabel") as! MCTPointLabel //line with error
    highscoreLabel.setTo(defaults.integerForKey("highscore"))

Really confused, need help figuring this out! Thanks in advance!

真的很困惑,需要帮助解决这个问题!提前致谢!

    **EDIT**: The declaration of highscoreLabel:
      func addPoints() {
    let pointsLabel = MCTPointLabel(num: 0)
    pointsLabel.fontColor = UIColor.brownColor()
    pointsLabel.position = CGPointMake(30.0, view!.frame.size.height - 40)
    pointsLabel.name = "pointsLabel"
    addChild(pointsLabel)


    let highScoreLabels = MCTPointLabel(num: 0)
    highScoreLabels.position = CGPointMake(view!.frame.size.width - 40, view!.frame.size.height - 40)
    addChild(highScoreLabels)

    let highscoreLabel = SKLabelNode(text: "High Score")
    highscoreLabel.fontColor = UIColor.brownColor()
    highscoreLabel.fontSize = 16.0
    highscoreLabel.fontName = "Chalkduster"
    highscoreLabel.name = "highscoreLabel"
    highscoreLabel.position = CGPointMake(620, 310)
    addChild(highscoreLabel)

   }

2 个解决方案

#1


0  

change inside of your GameScene the declaration for the High score label:

在GameScene内部更改高分标签的声明:

let highscoreLabel = MCTPointsLabel(text: "High Score")

Assuming that the MCTPointsLabel inherits from SKLabelNode everything will work fine. You have to make sure that if you force typecast a class that you really do in fact have an object of that class. When you said this:

假设MCTPointsLabel继承自SKLabelNode,一切都会正常工作。你必须确保如果你强制类型转换一个你真正拥有的类实际上有一个该类的对象。当你说到这个时:

let highscoreLabel = childNodeWithName("highscoreLabel") as! MCTPointLabel

You typecast the label as a MCTPointLabel, but because your highscoreLabel before was actually declared as an SKLabelNode, you weren't able to typecast and received the error.

您将标签类型化为MCTPointLabel,但由于之前的highscoreLabel实际上被声明为SKLabelNode,因此您无法进行类型转换并收到错误。

Hope this helps, let me know if you need clarification.
Edit: Try this instead:

希望这有帮助,如果您需要澄清,请告诉我。编辑:改为尝试:

var highscoreLabel = MCTPointsLabel(num: 0)
highscoreLabel.text = "High Score"

#2


0  

Clearly childNodeWithName("highscoreLabel") is not an MCTPointLable.

显然,childNodeWithName(“highscoreLabel”)不是MCTPointLable。

Force casts (with !) are generally avoidable and are likely to lead to runtime issues. You need to work out what the possible results of childNodeWithName("highscoreLabel") are and make sure that you handle all possibilities or find the information that you need another way.

强制转换(带!)通常是可以避免的,并且可能导致运行时问题。您需要弄清楚childNodeWithName(“highscoreLabel”)的可能结果是什么,并确保您处理所有可能性或找到您需要的另一种方式的信息。

#1


0  

change inside of your GameScene the declaration for the High score label:

在GameScene内部更改高分标签的声明:

let highscoreLabel = MCTPointsLabel(text: "High Score")

Assuming that the MCTPointsLabel inherits from SKLabelNode everything will work fine. You have to make sure that if you force typecast a class that you really do in fact have an object of that class. When you said this:

假设MCTPointsLabel继承自SKLabelNode,一切都会正常工作。你必须确保如果你强制类型转换一个你真正拥有的类实际上有一个该类的对象。当你说到这个时:

let highscoreLabel = childNodeWithName("highscoreLabel") as! MCTPointLabel

You typecast the label as a MCTPointLabel, but because your highscoreLabel before was actually declared as an SKLabelNode, you weren't able to typecast and received the error.

您将标签类型化为MCTPointLabel,但由于之前的highscoreLabel实际上被声明为SKLabelNode,因此您无法进行类型转换并收到错误。

Hope this helps, let me know if you need clarification.
Edit: Try this instead:

希望这有帮助,如果您需要澄清,请告诉我。编辑:改为尝试:

var highscoreLabel = MCTPointsLabel(num: 0)
highscoreLabel.text = "High Score"

#2


0  

Clearly childNodeWithName("highscoreLabel") is not an MCTPointLable.

显然,childNodeWithName(“highscoreLabel”)不是MCTPointLable。

Force casts (with !) are generally avoidable and are likely to lead to runtime issues. You need to work out what the possible results of childNodeWithName("highscoreLabel") are and make sure that you handle all possibilities or find the information that you need another way.

强制转换(带!)通常是可以避免的,并且可能导致运行时问题。您需要弄清楚childNodeWithName(“highscoreLabel”)的可能结果是什么,并确保您处理所有可能性或找到您需要的另一种方式的信息。