Store UIColor with UserDefaults in Swift 3
This article was originally published at iOS Geek Community.
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.
Prerequisite: Understand Type Casting from the bottom of your heart. In other words, be able to distinguish between as, as!, as? You can start off with this video where I show my face and speak English on YouTube.
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. The UserDefaults 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.