从NSUserDefaults读取数据

时间:2015-02-22 18:12:19

标签: ios swift nsuserdefaults

我在SpriteKit中用Swift写游戏。我有两个课程,一个名为“GameScene”和“PlyScene”。我想从“GameScene”类的“PlyScene”中读取高分,但是当我这样做时,我在这一行收到错误Thread 1:EXC_BREAKPOINT

scoreText1.text = String(score as NSString)

PlyScene中的Cod:

 func save() -> Int{

    if score2 < score{
    score2 = score
    println(score2)
    let defaults = NSUserDefaults.standardUserDefaults()
    defaults.setObject(score2, forKey: "score2")
    defaults.synchronize()

    }
    return 0
}

GameScene中的代码:

 let defaults = NSUserDefaults.standardUserDefaults()
    if let score: AnyObject = defaults.valueForKey("score2"){
        println("yes")
        scoreText1.text = String(score as NSString)

1 个答案:

答案 0 :(得分:0)

你的问题是施法。如果你想把它作为一个字符串,你应该像那样强制转换它:

scoreText1.text = String(score as Int)