Working with UserDefaults in iOS
Tags: userdefaults, swift, ios, storage
Published: 2023-12-22 12:49:26

---
title: Working with UserDefaults in iOS
published: true
date: 2023-12-22 12:20:21 UTC
tags: userdefaults, swift, ios, storage
canonical_url: https://blog.shivammaggu.com/working-with-userdefaults-in-ios
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ytswauf6k6l872bowgn.png
---

UserDefaults provides a database to store data and it is available as long as the app is installed.

## Table of Contents
- [Features](#features)
- [Usage](#usage)
  - [Utilising the default shared instance of UserDefaults.](#utilising-the-default-shared-instance-of-userdefaults)
  - [Creating a UserDefaults that can be shared with multiple apps and extensions.](#creating-a-userdefaults-that-can-be-shared-with-multiple-apps-and-extensions)
  - [Storing custom objects in UserDefaults.](#storing-custom-objects-in-userdefaults)
  - [Using UserDefaults as a Property Wrapper](#using-userdefaults-as-a-property-wrapper)

## Features

- The UserDefaults class provides access to the user's defaults database.

- It stores data persistently across the app launches as long as the app is installed.

- Access to the data is private to the app unless shared through App Groups.

- Access to this database is thread-safe.

- It is a key-value store that works through a property list (plist) file.

- Supported data types include String, Bool, Date, Array, Dictionary, Data and Numbers.

- Custom objects can be stored by encoding them as data.

- We can use this to store non-sensitive data such as user preferences.

## Usage

### Utilising the default shared instance of UserDefaults.

```
import Foundation
// Set a key value in UserDefaults
UserDefaults.standard.set("Shivam", forKey: "firstname")
// Get a key value from User
DefaultsUserDefaults.standard.value(forKey: "firstname")
// Empty User
DefaultsUserDefaults.standard.removeObject(forKey: "firstname")
```

### Creating a UserDefaults that can be shared with multiple apps and extensions.

```
import Foundation
// User Defaults with Suite.
// Useful for sharing data with other apps
// or extensions with app groups.
let defaults = UserDefaults(suiteName: "group.com.organisation.appname")!
// Set a key value in User Defaults
defaults.set("Shivam", forKey: "firstname")
// Get a key value from User Defaults
defaults.value(forKey: "firstname")
// Empty User Defaults
defaults.removeObject(forKey: "firstname")
```

### Storing custom objects in UserDefaults.

```
import Foundation

// Custom model to be stored in UserDefaults
struct Student: Codable {
    let firstname: String
    let lastname: String
    let grade: Int
    let subjects: Array<String>
    let teachers: [String: String]
    let profileImage: Data
    let dateModified: Date
}

// Student Object
let student = Student(firstname: "John",
                      lastname: "Doe",
                      grade: 2,
                      subjects: ["Hindi", "English", "Maths"],
                      teachers: ["Hindi": "Teacher A", "English": "Teacher b", "Maths": "Teacher X"],
                      profileImage: Data(),
                      dateModified: Date())

// Encode custom object to data and store it in UserDefaults
let encoder = JSONEncoder()
do {
    let data = try encoder.encode(student)
    UserDefaults.standard.setValue(data, forKey: "StudentData")
} catch {
    throw error
}

// Retrieve data from UserDefaults and decode it to custom object
let decoder = JSONDecoder()
if let data = UserDefaults.standard.value(forKey: "StudentData") as? Data {
    do {
        let object = try decoder.decode(Student.self, from: data)
        print(object)
    } catch {
        throw error
    }
} else {
    print("No data in Defaults.")
}
```

### Using UserDefaults as a Property Wrapper

Check out this [gist to use UserDefaults with Property Wrapper](https://gist.github.com/shivammaggu/451e06aafc8b25ba8ca9479a8f9ab175). This is a more generalized way to use in Projects.

{% gist https://gist.github.com/shivammaggu/451e06aafc8b25ba8ca9479a8f9ab175 %}