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.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
UserDefaultable.playground
LICENSE
README.md

README.md

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)