Follow along at https://www.hackingwithswift.com/100/49.
This day covers the second and final part of
Project 12: User Defaults
in Hacking with Swift.Project 12 is a technique project — geared towards refactoring Project 10 to use
UserDefaults
. You can find my original version of Project 10 in Day 42. However, I also copied everything over to Day 48's folder to extend from where I left off.With that in mind, Day 49 focuses on fixing Project 10's lack of data persistence by using
UserDefaults
andCodable
— and then concluding with some challenges around implementingUserDefaults
in a number of other projects.
Unlike NSCoding
, Codable
allows us to persist data as JSON, which makes our data much more portable across different domains. If we don't need our types to interface to Objective-C, Codable
is likely the way to go.
Differences aside, though, the underlying philosophies are very similar: Encoding and decoding... serializing and deserializing... and hooking in to the transformation process with the way we define our models.
But where Codable
really shines is in practice. When we have an object structure that doesn't require custom key coding, Codeable
handles most of the serialization/deserialization under the hood. Case in point: our new Person
class:
class Person: NSObject, Codable {
var name: String
var imageName: String
init(name: String, imageName: String) {
self.name = name
self.imageName = imageName
}
}
External code that triggers the encoding or decoding is also much cleaner:
let encoder = JSONEncoder()
do {
let data = try encoder.encode(people)
userDefaults.set(data, forKey: "people")
} catch {
print("Failed to saved people")
}
let decoder = JSONDecoder()
if let peopleData = userDefaults.object(forKey: "people") as? Data {
do {
return try decoder.decode([Person].self, from: peopleData)
} catch {
showError(error, title: "Error while loading saved people")
}
}
Modify project 1 so that it remembers how many times each storm image was shown – you don’t need to show it anywhere, but you’re welcome to try modifying your original copy of project 1 to show the view count as a subtitle below each image name in the table view.
- 🔗 Commit
Modify project 2 so that it saves the player’s highest score, and shows a special message if their new score beat the previous high score.
- 🔗 Commit
Modify project 5 so that it saves the current word and all the player’s entries to UserDefaults, then loads them back when the app launches
- 🔗 Commit