I just released an app to the app store and one of my patrons let me know that I should change the data type that I was previously storing as an Integer, using the NSKeyedArchiver, to a Double.
Easy enough to change the app's data model but when I reload the app on my test device, the NSKeyedUnarchiver obviously doesn't want to decode an Integer as a Double and throws an NSInvalidUnarchiveOperation Exception.
I was wondering how any other iOS Dev's would handle this situation. I'd hate to erase all of my users' previously saved data but that's the only solution I'm seeing.
My code is posted below. I've commented out a few solutions I tried to no avail
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)
}