Swift SpriteKit:线程 1:SIGABRT 信号。无法转换 SKLabelNode 类型的值?
Swift SpriteKit: Thread 1: signal SIGABRT. Cannot cast value of type SKLabelNode?
所以我正在完成一个项目,当我 运行 它崩溃了,我得到了线程 1:信号 SIGABRT 错误,我查看了描述,它给了我这个:
无法将类型 'SKLabelNode' (0x108ed0b78) 的值转换为 'Koala_Hop.MCTPointLabel' (0x108091da0)。
(lldb)
对于这行代码:
func loadHighscore() {
let defaults = NSUserDefaults.standardUserDefaults()
let highscoreLabel = childNodeWithName("highscoreLabel") as! MCTPointLabel //line with error
highscoreLabel.setTo(defaults.integerForKey("highscore"))
真的很困惑,需要帮助解决这个问题!提前致谢!
**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)
}
显然 childNodeWithName("highscoreLabel")
不是 MCTPointLable。
强制转换(使用 !
)通常是可以避免的,并且可能会导致运行时问题。您需要弄清楚 childNodeWithName("highscoreLabel")
的可能结果是什么,并确保您处理所有可能性或以其他方式找到您需要的信息。
在您的 GameScene 内部更改高分标签的声明:
let highscoreLabel = MCTPointsLabel(text: "High Score")
假设 MCTPointsLabel 继承自 SKLabelNode
一切都会正常工作。你必须确保,如果你强制类型转换一个 class,那么你实际上拥有一个 class 的对象。当你这样说时:
let highscoreLabel = childNodeWithName("highscoreLabel") as! MCTPointLabel
您将标签类型转换为 MCTPointLabel
,但由于您之前的 highscoreLabel 实际上被声明为 SKLabelNode
,您无法进行类型转换并收到错误。
希望这对您有所帮助,如果您需要说明,请告诉我。
编辑:试试这个:
var highscoreLabel = MCTPointsLabel(num: 0)
highscoreLabel.text = "High Score"
所以我正在完成一个项目,当我 运行 它崩溃了,我得到了线程 1:信号 SIGABRT 错误,我查看了描述,它给了我这个:
无法将类型 'SKLabelNode' (0x108ed0b78) 的值转换为 'Koala_Hop.MCTPointLabel' (0x108091da0)。 (lldb)
对于这行代码:
func loadHighscore() {
let defaults = NSUserDefaults.standardUserDefaults()
let highscoreLabel = childNodeWithName("highscoreLabel") as! MCTPointLabel //line with error
highscoreLabel.setTo(defaults.integerForKey("highscore"))
真的很困惑,需要帮助解决这个问题!提前致谢!
**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)
}
显然 childNodeWithName("highscoreLabel")
不是 MCTPointLable。
强制转换(使用 !
)通常是可以避免的,并且可能会导致运行时问题。您需要弄清楚 childNodeWithName("highscoreLabel")
的可能结果是什么,并确保您处理所有可能性或以其他方式找到您需要的信息。
在您的 GameScene 内部更改高分标签的声明:
let highscoreLabel = MCTPointsLabel(text: "High Score")
假设 MCTPointsLabel 继承自 SKLabelNode
一切都会正常工作。你必须确保,如果你强制类型转换一个 class,那么你实际上拥有一个 class 的对象。当你这样说时:
let highscoreLabel = childNodeWithName("highscoreLabel") as! MCTPointLabel
您将标签类型转换为 MCTPointLabel
,但由于您之前的 highscoreLabel 实际上被声明为 SKLabelNode
,您无法进行类型转换并收到错误。
希望这对您有所帮助,如果您需要说明,请告诉我。
编辑:试试这个:
var highscoreLabel = MCTPointsLabel(num: 0)
highscoreLabel.text = "High Score"