UserDefaults in Swift

Updated 28 February 2022

Save

In this article, we will be discussing UserDefaults in swift from scratch to the advance.

Firstly, we would explore what UserDefaults is in Swift. After that, we will discuss how we can take advantage of the UserDefaults. We will also know how the UserDefaults works internally

So let’s dive into the article.

UserDefaults in Swift Overview

Firstly, we are going to discuss how UserDefaults works internally.

UserDefaults uses the .plist file to store the data in key-value pairs.

Swift uses suites/domains to save data. Each suite has its own .plist file.

By default, every app has 8 suites which are listed below.

ADD_DOMAIN(_CFPreferencesStandardDomain(appPreferences->_appName, kCFPreferencesCurrentUser, kCFPreferencesCurrentHost));

ADD_DOMAIN(_CFPreferencesStandardDomain(appPreferences->_appName, kCFPreferencesCurrentUser, kCFPreferencesAnyHost));

ADD_DOMAIN(_CFPreferencesStandardDomain(kCFPreferencesAnyApplication, kCFPreferencesCurrentUser, kCFPreferencesCurrentHost));

ADD_DOMAIN(_CFPreferencesStandardDomain(kCFPreferencesAnyApplication, kCFPreferencesCurrentUser, kCFPreferencesAnyHost));

ADD_DOMAIN(_CFPreferencesStandardDomain(appPreferences->_appName, kCFPreferencesAnyUser, kCFPreferencesCurrentHost));

ADD_DOMAIN(_CFPreferencesStandardDomain(appPreferences->_appName, kCFPreferencesAnyUser, kCFPreferencesAnyHost));

ADD_DOMAIN(_CFPreferencesStandardDomain(kCFPreferencesAnyApplication, kCFPreferencesAnyUser, kCFPreferencesCurrentHost));

ADD_DOMAIN(_CFPreferencesStandardDomain(kCFPreferencesAnyApplication, kCFPreferencesAnyUser, kCFPreferencesAnyHost));

The suites are organized into search-list, so when we perform read/write operation the search list is initialized for the first time.

We can add as many suites as we want, and customize the UserDefaults according to our requirements.

You can define your suite as below

To store the values in UserDefaults CFPreferencesSetAppValue(_:_:_:) function is used. For instance, you can declare the function as

Note: UserDefaults works best when the read operations are frequent and write operations are less.

Creating UserDefaults using Property Wrapper

The above code uses the PropertyListValue protocol.

You can create the structure for Key as below.

After that, you can extend the Key struct for your statically defined key values.

Read, Write, Delete and Update operations for UserDefaults in Swift

You can perform Write operations as

After that, you can perform the Read operation as

Similarly, We can remove or delete the saved data.

To update the value for a key in UserDefault just set another value in the same key as the Write operation.

Conclusion

To go more deep inside the UserDefaults you can refer to the below link

Advanced UserDefaults

Please read out more interesting articles here.

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home