3 回答

TA貢獻(xiàn)1824條經(jīng)驗 獲得超6個贊
let age = aDecoder.decodeObject(forKey: "age") as! Int
Swift 3對此進(jìn)行了更改;這不再適用于值類型?,F(xiàn)在正確的語法是:
let age = aDecoder.decodeInteger(forKey: "age")
有針對各種不同類型的關(guān)聯(lián)的encode ...()函數(shù):
let myBool = aDecoder.decodeBoolean(forKey: "myStoredBool")
let myFloat = aDecoder.decodeFloat(forKey: "myStoredFloat")
編輯:Swift 3中所有可能的encodeXXX函數(shù)的完整列表
編輯:
另一個重要的注意事項:如果您以前保存過使用較早版本的Swift編碼的數(shù)據(jù),則必須使用encodeObject()解碼這些值,但是一旦使用encode(...)重新編碼數(shù)據(jù),就不能再使用如果它是值類型,則使用decodeObject()解碼。因此,Markus Wyss的答案將允許您處理使用兩種Swift版本對數(shù)據(jù)進(jìn)行編碼的情況:
self.age = aDecoder.decodeObject(forKey: "age") as? Int ?? aDecoder.decodeInteger(forKey: "age")
- 3 回答
- 0 關(guān)注
- 877 瀏覽
添加回答
舉報