A blog post about how to better deal with UserDefaults and stringly typed APIs
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


Swift: UserDefaults Protocol

Swift eye for the stringly typed API

Swift 3 brought a tsunami of changes to the language as well as our codebase, some of you reading this may even still be battling with the migration too. But even with all these changes, we’re still left with some APIs within Foundation that are stringly typed, which is totally fine… Until it’s not.

Read the full article on Medium


tl;dr: Turn these

// Set
UserDefaults.standard.set(true, forKey: Constants.Account.isUserLoggedIn.rawValue)

// Get
UserDefaults.standard.bool(forKey: Constants.Account.isUserLoggedIn.rawValue)

Into this

// Set
UserDefaults.Account.set(true, forKey: .isUserLoggedIn)

// Get
UserDefaults.Account.bool(forKey: .isUserLoggedIn)