Store UIColor with UserDefaults in Swift 4

Store anything offline with two lines of code

Bob Lee
Bob the Developer
4 min readDec 21, 2016

--

Will Find Out

Last update on May 14th, 2017 | Swift 3.1

Christmas is coming, and we are learning Swift. Warren Buffer once said, “The more you learn, the more you earn. Anyway, today, I wanna sound a little casual because I just feel like it, and since I’m the only writer, I don’t think this article will be rejected 😎.

So, what the heck is UserDefaults in the first place? Why is the name so ugly? Why are we using it? and Why am I writing about it? If you can give at least one answer to these questions, you may skip to Part 2 where I talk about UIColor. If not, let’s earn more.

Prerequisite: Understand Type Casting from the bottom of your heart. In other words, be able to distinguish between as, as!, as?. You might want to Learn Swift with Bob, but it’s up to you.

Stage One: Analogy

As a tradition, let’s start off some funky and tangible ways to understand UserDefaults at an extremely high level. Actually, this is too simple. I don’t think it’s necessary. TheUserDefaults object saves user data. So that when you first download an app, you can save preferences such as a background color/image even when the battery kills itself. It can save ALL kinds of things. If you have 254GB of free space on your phone, it can save 254GB of user data. But, there is a big problem.

It regurgitates everything during the runtime. Okay, the previous sentence can be a bit ambiguous. Let’s try this. It will vomit everything out when you first launch an app or the view is loaded. Hmm, 🤔. Here is the better way. It’s like you running to the bathroom and taking a poop that you’ve been holding for 5 days at once. What happens to your body? You get overwhelmed. It may not even come out right, and most importantly, it will hurt you real bad. Same thing, you want to make sure you only carry enough of poop inside of the large intestine so that you (iPhone) can take care of and handle like a boss.

LOL. Yeah, for those newcomers, this is how I think and execute. Excuse me. No one can stop me.

Sure, there is another way to go about in order to solve this serious problem both for you and the phone. We can use CoreData. You’ve probably heard it before. I will publish one on this Saturday 8am. Stay tuned.

Do I even deserve a ❤️ for publishing on Christmas Eve which is a Saturday night for me?

Stage Two: Let’s Get Real

We’ve had enough of fun (at least for me). It’s time to dive in. As usual, we have to create an instance/object of UserDefaults like this

Now, the object is able to take intake a bunch of value, to be exact, a bunch of poop and each poop has its own name. For example, let’s try to store an Int value.

I think the poop analogy is a little bit too disturbing, so let’s change the analogy to a cute hamster like below.

UserDefault Object is similar to a hamster. If you have too much stuff inside, life gets a bit harder

Okay, so, it should look something like this,

Yes, you may change the designated value of myAge of the app if your daddy wants to change the value.

That’s it. You’ve written just two lines of code to save data offline with UserDefaults. You can save things like gender, blood type, height, weight, show size, game level. But, of course, this isn’t the end of the story. Just like hamsters/humans, they can’t eat all kinds of things like rock and diamond. You don’t feed rice to a 15 day year old. Do you? (Dear, Apple UIKit engineers, why can’t we…?)

But, surprisingly, you can vomit all kinds of things. I don’t know why. I guess it has to do with how our body kinda works like that.

Another problem, how about storing/retrievingDictionary? There isn’t a direct method to set a dictionary value. But, of course, there are two ways.

First Method

The content has been fully migrated from Medium to the personal blog. If you wish to finish reading the rest of the article and learn about storing UIColor and data, feel free to visit the new platform here.

--

--