How to write strong UserDefaults class?

Sajjad Sarkoobi
Sep 3, 2018 · 4 min read

iOS, swift | accessing user default objects | read write data easily

The simplest way to store current user data in iOS programming is UserDefaults. for example with this approach you can store current user name , user age or many other details of that user. then you can update them ,retrieve them and reuse them within your application.

but how you can write a global class for it and call each parameter just as a global variable?

I’m going to create a userHolder class and describe everything here.

we can start with a simple struct, I call it “userData”. then in that struct we should define our instance of UserDefaults.

struct userData {
private let defaultStorage = UserDefaults.standard
}

we are using “UserDefaults.standard “ here, but we have another option too, we can make so many instance from userdefaults by assigning different suiteNames. so each instance can has its own properties without messing things around and overriding in their own properties. like this:

struct userData {
private let defaultStorage = UserDefaults(suiteName: "com.yourcompany.appname")
}

its better to use our unique bundle identifier as suiteName or something related to it and unique.

in this post we are assuming “UserDefaults.standard”.

Now we should define our keys. you can add as many as your application need. something like this :

enum UserDefaultsKeys:String {
case userToken
case name
case age
case avatarLink
}

now we need 2 private functions for putting data into storage and also retrieving data from it . I’m suggesting to make them private because at the end of this post you can see that there is no need of them in other classes.

as you can see both of these functions need a key for assigning or retrieving value from them. in “getFromdefaultStorage” function we are using Optional Binding (if -let statements) because if there is any problem with the name of the key or the key doesn’t exist it will return nil, and we really want to avoid nil in our application. so we will receive empty String if there is any error.

we are going to define our user data variables now. all of them will be some compute properties that are going to use “UserDefaultsKeys” for setting and getting data.

compute properties for user objects

as you can see these properties call our private functions when getting data or setting data to them. we need to access these properties in all classes. so we are going to define a strong instance at top of the “userData” struct and call it “UserHolder”:

Strong instance of user data

so now in other classes we can simply call any properties of the UserHolder and trying to set or get data from it:

UserHolder.name = "Sajjad"print("age is \(UserHolder.age)")

all done! :) now we have access to our user defaults data all over the app.

How to delete all data? | LogOut User

just one thing remained, what if we want to delete all the user data or we want to LogOut that user, how to clean our stored data?. we should just add another simple function and call it “deleteAllData” :

now simply we can just call:

UserHolder.deleteAllData()

to delete all the data stored in userdefaults.

this is the complete code: (hope it is useful)

you can find my other post about How to write strong models for swift Codable and reusing that model again. here:

later I will describe how you can store JSON data in userDefaults and retrieve it as a Model directly from UserHolder. so stay tuned! :)

Thank you for reading.