What is SwiftyUserDefaults and why do you need it?
\n\nSwiftyUserDefaults is a lightweight but powerful wrapper library for the standard UserDefaults class in Swift. If you've ever worked with UserDefaults directly, you know it can be tedious: you need to write a lot of boilerplate code to save and retrieve values, explicitly specify the data type, use string keys (which are prone to typos), and handle optional values.
SwiftyUserDefaults solves all these problems. It provides:
\n- \n
- Static typing: the compiler checks types, eliminating runtime errors. \n
- Global access: access values through the convenient
Defaultsproperty. \n - Custom type support: you can save any of your own structures and classes by implementing the
DefaultsSerializableprotocol. \n - Syntactic sugar: working with UserDefaults becomes similar to working with regular properties. \n
- Safety: the risk of typos in string keys is eliminated. \n
The library is ideal for storing small amounts of data: application settings, user interface state, authentication tokens, etc. It is not intended for storing large data arrays — for that, use Core Data, Realm, or files.
\n\nInstallation
\n\nSwiftyUserDefaults is distributed via Swift Package Manager (SPM), CocoaPods, and Carthage. Let's look at the two most popular methods.
\n\nSwift Package Manager (recommended)
\n- \n
- In Xcode, open the menu:
File → Add Packages...\n - Paste the repository URL:
https://github.com/sunshinejr/SwiftyUserDefaults.git\n - Select the version (the latest stable version is recommended) and click
Add Package. \n - Make sure the package is added to your target. \n
CocoaPods
\nAdd to your Podfile:
target 'YourAppTarget' do\n use_frameworks!\n pod 'SwiftyUserDefaults', '~> 5.3'\nend\nThen run pod install.
After installation, import the library in the required file:
\nimport SwiftyUserDefaults\n\nQuick Start
\n\nLet's jump straight into the code. Here's what a typical workflow with UserDefaults looks like without SwiftyUserDefaults and with it.
\n\nWithout SwiftyUserDefaults (standard approach)
\n// Saving\nUserDefaults.standard.set("John", forKey: "user_name")\nUserDefaults.standard.set(25, forKey: "user_age")\nUserDefaults.standard.set(true, forKey: "is_logged_in")\n\n// Reading\nlet name = UserDefaults.standard.string(forKey: "user_name") ?? ""\nlet age = UserDefaults.standard.integer(forKey: "user_age")\nlet isLoggedIn = UserDefaults.standard.bool(forKey: "is_logged_in")\n\nWith SwiftyUserDefaults
\n// Define keys (can be in a separate file)\nextension DefaultsKeys {\n var userName: DefaultsKey<String> { .init("user_name") }\n var userAge: DefaultsKey<Int> { .init("user_age") }\n var isLoggedIn: DefaultsKey<Bool> { .init("is_logged_in") }\n}\n\n// Saving\nDefaults[\\.userName] = "John"\nDefaults[\\.userAge] = 25\nDefaults[\\.isLoggedIn] = true\n\n// Reading\nlet name: String = Defaults[\\.userName]\nlet age: Int = Defaults[\\.userAge]\nlet isLoggedIn: Bool = Defaults[\\.isLoggedIn]\n\nprint("\\(name) is \\(age) years old. Logged in: \\(isLoggedIn)")\n\nAs you can see, the code is cleaner, types are checked at compile time, and keys are defined in one place.
\n\nMain Methods and Functions
\n\nLet's look at 10 key features of the library with examples.
\n\n