Jun 16, 2015:  ~ 2 min read

[EP] Migrating old data

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 laugh, it was a long time ago :)
unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData: dat];
kConstants.allTheData = [unarchiver decodeObjectForKey: @"dic"];
[unarchiver finishDecoding];


let unarchiver = NSKeyedUnarchiver(forReadingWithData: decryptedData)

if let events = unarchiver.decodeObject(forKey: "data") as? [EPEvent] {
  EPEvent.data = events
else if let oldData = unarchiver.decodeObject(forKey: "dic") as? [Any] {
else {
  EPEvent.data = []


where migrateOldData is:

// The fallbacks should never happen, but it doesn't hurt to be extra sure.
for event: [Any] in data {
  let event = 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? DateComponents ?? EPEvent.alertTime,
    reminderComponents: event[4] as? DateComponents ?? 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:

// -- Optional("Daily")
Subscribe to my monthly newsletter.
No spam, unsubscribe at any time.