Member-only story

How to check if UserDefaults object is Empty

Jay Wilson
1 min readMay 13, 2019

This will be a guide to check if UserDefaults has been set.

You will want to use this to make sure a value is not nil before you use it.

Here is a simple example in a ViewController class.

  1. Create a constant to interact with the UserDefaults
  2. Create an if statement to check if the object does not exist.
  3. If the object does not exist, then set a default option. In the gist above, I set it to dark.

There is no need to set the default option if the default object does exist because it has been set before.

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Jay Wilson
Jay Wilson

Written by Jay Wilson

I like to solve problems using Swift, make YouTube videos, and take photos. I also really enjoy a good cup of coffee.

No responses yet

Write a response