[EP] Migrating old data

:  ~ 1 min read

First things first, I want to migrate old users' data to Swift objects:

// Didn't want to use CoreData just for one object, so I was creating an array of arrays: 
// array[0] = recurring time, array[2] = name, etc. 
// Don't judge, it was a long time ago :)
unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData: dat];
kConstants.allTheData = [unarchiver decodeObjectForKey: @"dic"];
[unarchiver finishDecoding];

to:

let unarchiver = NSKeyedUnarchiver(forReadingWithData: decryptedData)

if let arr: AnyObject = unarchiver.decodeObjectForKey("data") {
  EPEvent.data = arr as! [EPEvent]
}
else if let oldData: AnyObject = unarchiver.decodeObjectForKey("dic") {
  Utils.migrateOldData(oldData as! [AnyObject])
}
else {
  EPEvent.data = []
}

unarchiver.finishDecoding()

where migrateOldData is:

// The fallbacks should never happen, but it doesn't hurt to be extra sure.
for event: AnyObject in data {
  EPEvent.data << EPEvent(
    recurring: event[0] as? String ?? EPEvent.recurring,
    sum: event[1] as? Float ?? 0.0,
    name: event[2] as? String ?? "Invalid data",
    eventComponents: event[3] as? NSDateComponents ?? EPEvent.alertTime,
    reminderComponents: event[4] as? NSDateComponents ?? EPEvent.alertTime,
    fireTime: event[5] as? String ?? "Day of event",
    repeats: event[6] as? String ?? "None"
  )
}

saveData() // This just writes to file, encrypted

Trying it out:

print(EPEvent.data.first?.recurring)
// -- Optional("None")