2017-08-17 82 views
0

嘗試使用keyValue對從iCloud檢索int。有人可以向我解釋爲什麼這是崩潰的錯誤:iCloud keyValue使我的遊戲崩潰

'[<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

value(forKey :)用於動態訪問對象的實例變量。 I. e。鍵 - 值編碼。

該對象上沒有名爲「current」的實例變量。你想訪問商店中存儲的值,根據文檔應該使用object(forKey :)來完成。