SwiftyUserDefaults for Swift: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is SwiftyUserDefaults and why do you need it?

\n\n

SwiftyUserDefaults 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.

\n\n

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 Defaults property.
  • \n
  • Custom type support: you can save any of your own structures and classes by implementing the DefaultsSerializable protocol.
  • \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
\n\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\n

Installation

\n\n

SwiftyUserDefaults is distributed via Swift Package Manager (SPM), CocoaPods, and Carthage. Let's look at the two most popular methods.

\n\n

Swift Package Manager (recommended)

\n
    \n
  1. In Xcode, open the menu: File → Add Packages...
  2. \n
  3. Paste the repository URL: https://github.com/sunshinejr/SwiftyUserDefaults.git
  4. \n
  5. Select the version (the latest stable version is recommended) and click Add Package.
  6. \n
  7. Make sure the package is added to your target.
  8. \n
\n\n

CocoaPods

\n

Add to your Podfile:

\n
target 'YourAppTarget' do\n  use_frameworks!\n  pod 'SwiftyUserDefaults', '~> 5.3'\nend
\n

Then run pod install.

\n\n

After installation, import the library in the required file:

\n
import SwiftyUserDefaults
\n\n

Quick Start

\n\n

Let's jump straight into the code. Here's what a typical workflow with UserDefaults looks like without SwiftyUserDefaults and with it.

\n\n

Without 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\n

With 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\n

As you can see, the code is cleaner, types are checked at compile time, and keys are defined in one place.

\n\n

Main Methods and Functions

\n\n

Let's look at 10 key features of the library with examples.

\n\n

1. Defaults[\\.key] — getting

Recommendations