アプリケーションストアにアプリをリリースしたばかりで、以前にNSKeyedArchiverを使用して以前にIntegerとして保存していたデータ型をダブル。NSKeyedArchiverオブジェクトを新しいバージョンに変換する方法
アプリケーションのデータモデルを変更するのは簡単ですが、テストデバイスでアプリケーションをリロードすると、NSKeyedUnarchiverは明らかにIntegerをDoubleとしてデコードしないようにし、NSInvalidUnarchiveOperation例外をスローします。
他のiOSデベロッパーがこの状況をどのように処理するのだろうと思っていました。以前に保存したユーザーのデータをすべて消去するのは嫌ですが、それは私が見ている唯一の解決策です。
マイコードは以下のとおりです。私はあなたが、アプリケーションの負荷として、ソートのアーカイブをアップグレードするための機能を確認する必要がありIntegerなどのキーで読み、そしてそれを書くかもしれない、私は無駄
required convenience init?(coder aDecoder: NSCoder){
func decodeDoubles(coder aDecoder: NSCoder) throws-> (Double, Double){
print("Getting in here")
/* These are stored as Integers in previous version */
let myEarned = aDecoder.decodeDoubleForKey(PropertyKey.earnedKey)
let myTotal = aDecoder.decodeDoubleForKey(PropertyKey.totalKey)
/* App Crashes here - exception not caught */
print("After decode attempt")
return (myEarned, myTotal)
}
let myID = aDecoder.decodeIntegerForKey(PropertyKey.idKey)
let myName = aDecoder.decodeObjectForKey(PropertyKey.nameKey) as! String
let myWeight = aDecoder.decodeIntegerForKey(PropertyKey.weightKey)
/* Throws exception */
//let myEarned = aDecoder.decodeDoubleForKey(PropertyKey.earnedKey)
//let myTotal = try! aDecoder.decodeDoubleForKey(PropertyKey.totalKey)
var myEarned: Double = 0
var myTotal: Double = 0
do {
(myEarned, myTotal) = try decodeDoubles(coder: aDecoder)
} catch {
print("Exception caught - \(error)")
myEarned = Double(aDecoder.decodeIntegerForKey(PropertyKey.earnedKey))
myTotal = Double(aDecoder.decodeIntegerForKey(PropertyKey.totalKey))
}
self.init(id: myID, name: myName, weight: myWeight, earned: myEarned, total: myTotal)
}
それは私がそれをやる方法です。 –
これは素晴らしいアイデアです!私はそのことを考えましたが、整数をdoubleとしてデコードしようとしたときに同じ問題が発生するのを避けるために、どこにフラグを格納できるかは正確にはわかりませんでした。 NSUserDefaultsに値を保存したり、NSKeyedArchiverにフラグ値を追加しようとしている可能性があります。助言がありますか? – Louie4
NSUserDefaultsにフラグを保存するほうが簡単でしょう。また、関数内でtry/catchを行うことで、整数で読み込むことができない場合、新しいユーザーとフラグをマークして、再度試してみるようにします(または、値が何も表示されず、とにかくアップグレードしてください) – Fonix