Swift 3: Permanently storing data

The one with UserDefaults.

Feb 5, 2017 · 3 min read

Permanent data storage is an important skill to have under your belt as an iOS app developer. For most apps you are going to want to store some information about the user so that when they come back to the app they get their data straight away even after closing the app. There are number of ways to achieve this, we are looking at one of the simplest one in this section.

So what we are going to be using here is something called UserDefaults. Which as you might imagine is used to store some basic information about that particular user but really you can use to store any kind of information locally on the device.

This is very easy to setup as well. Open up a new xcode project and type the following code below in the viewDidLoad() method.

We are saving the string(or any item) to UserDefaults which sets a variable called “key” and gives it a value “String” and when we want to bring that value back we just use the “key” which can be anything. When you run the app you will have your string printed out in the logs.

Now just to make sure that this has indeed saved data permanently let’s run the app again but this time we will comment out the saving part. This does the exact same thing (check logs again), which means that the data is stored permanently and we can access it whenever we load the app.

Basically you can store any kind of data into UserDefaults as in Objects like String, Date, Array, Integer and even Boolean. For that we need to create a global instance of UserDefaults.

Again comment out the storing values part and run the app, you will find the data is stored permanently.


When retrieving objects, the result is optional. This means you can either accept the optionality, or typecast it to a non-optional type and use the nil coalescing operator to handle missing values. For example:

So what we are checking here is are we able to take our name object and cast it to a string and if we are, then we print it. This is a much safer way of checking if the object exists and whether it’s of a certain type. That’s all about UserDefaults.

Note: NSUserDefaults has been renamed to UserDefaults standardUserDefaults() has been renamed to standard


Aviacommerce is an open source e-commerce framework with an aim to simplify e-commerce ecosystem by creating and unifying smart services and providing consistent, cost-effective solution to sellers helping them sell more. visit aviacommerce.org to know more.

click the image to know more or visit https://aviacommerce.org


Aviabird Technologies provides enterprise software consulting in Angular, Rails, Elixir, Golang, React, Flutter and other technologies.


Written by


a “work in progress”



Aviabird Technologies provides enterprise software consulting in Angular, Rails, Elixir, Golang, React, Flutter and other technologies.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade