sunshinejr / SwiftyUserDefaults

Modern Swift API for NSUserDefaults
http://radex.io/swift/nsuserdefaults/static
MIT License
4.85k stars 365 forks source link

Adding a new property to a custom class erases all stored data #249

Closed Xedart closed 4 years ago

Xedart commented 4 years ago

Suppose I have a class

class Document: Codable, DefaultsSerializable { 
    var title: String
}

static var documents: DefaultsKey<[Document]> {return .init("documents", defaultValue: [])}

Then I need to add a new property to the class Document:

class Document: Codable, DefaultsSerializable { 
    var title: String
    var modifiedDate: Date
}

When I do so, all previously stored documents are erased. Is there a way to do something about it?

sunshinejr commented 4 years ago

Hey @Xedart, yeah the migration between versions is kinda tricky. There are few ways of fixing this one, though:

Xedart commented 4 years ago

@sunshinejr Yup, custom Codable init solved the issue. Thanks!

sunshinejr commented 4 years ago

@Xedart awesome 🚀