Store UIColor with UserDefaults in Swift 4
Store anything offline with two lines of code
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
let defaults = UserDefaults.standard
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.
defaults.set(20, forKey: "myAge")
I think the poop analogy is a little bit too disturbing, so let’s change the analogy to a cute hamster like below.
Okay, so, it should look something like this,
class ViewController: UIViewController {
let defaults = UserDefaults.standard override func viewDidLoad() {
super.viewDidLoad()
defaults.set(20, forKey: "myAge")
print(defaults.integer(forKey: "myAge"))
}
}
Yes, you may change the designated value of myAge
of the app if your daddy wants to change the value.
defaults.set(47, forKey: "myAge")
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…?)
// You can only feed these
func set(Bool, forKey: String)
func set(Float, forKey: String)
func set(Int, forKey: String)
func set(Any?, forKey: String)
func set(Double, forKey: String)
func set(URL?, forKey: String)
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.
// Catharsis
func array(forKey: String)
func bool(forKey: String)
func data(forKey: String)
func dictionary(forKey: String)
func float(forKey: String)
func integer(forKey: String)
func object(forKey: String)
func stringArray(forKey: String)
func string(forKey: String)
func double(forKey: String)
func url(forKey: String)
func value(forKey: String)
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
let name = ["Real": "SangJoon Lee"]
defaults.set(name, forKey: "name") // Store as Any?if let name = defaults.value(forKey: "name") as? [String: String] {
print(name) // Downcast from Any? to [String: String]
}// ["Real": "SangJoon Lee"]