"动态铸造失败失败"来自AnyObject?到Int

时间:2014-08-28 21:37:51

标签: casting swift optional

Nutshell - 我正试图从plist中获得高分

以下是读取数据的代码:

    var myOutput: AnyObject? = NSUserDefaults.standardUserDefaults().objectForKey("highscore")
    println(myOutput!)

这是成功的,println的结果是正确的数据

    if myOutput != nil{
        highscore = myOutput! as Int
    }

这给了我一个“Swift动态演员失败”。从我读过的所有内容来看,这应该是有用的,所以任何提示都会很棒。

1 个答案:

答案 0 :(得分:1)

感谢user2864740让我走上正轨。正确的解决方案是:

    if myOutput != nil{
        highscore = myOutput!.integerValue
    }
相关问题