iCloud keyValue崩溃了我的游戏

时间:2017-08-17 00:34:05

标签: swift sprite-kit icloud

尝试使用keyValue对从iCloud中检索int。有人可以向我解释为什么这会导致错误:

 '[<NSUbiquitousKeyValueStore 0x170287e40> valueForUndefinedKey:]: this class is not key value coding-compliant for the key current.'

我以前的项目工作得很好。

 var iCloudKeyStore: NSUbiquitousKeyValueStore = NSUbiquitousKeyValueStore()

 func loadSavedData() {

    if let cloudCurrent = iCloudKeyStore.value(forKey: "current") as! Int? { /* <-------- Crashes on this line */
        print("Current Level Found From iCloud: \(cloudCurrent)")
        currentLevel = cloudCurrent
    }else{
        print("No Current Level Found")
    }

    if let cloudAchieved = iCloudKeyStore.value(forKey: "achieved") as! Int? {
        print("Current Level Found From iCloud: \(cloudAchieved)")
        levelAchieved = cloudAchieved
    }else{
        print("No Achieved Level Found")
    }

1 个答案:

答案 0 :(得分:1)

value(forKey :)用于动态访问对象的实例变量。 I. e。键 - 值编码。

没有名为&#34; current&#34;的实例变量。在那个对象上。您想要访问存储在商店中的值,根据文档应该使用object(forKey :)来完成。