Google is your friend!

Hi all,

Today I spent a bunch of hours trying to get UIColors to fill the backgrounds of a pickerView as well as how to save the choice to disc since both Realm (my go-to solution for persistent data) and UserDefaults both can not save UIColor type.

There are no tutorials that I could find, so I spent hours and hours scouring dozens of articles from people with very smart brains. I eventually came up with a solution, and I can check that off the todo list, and this upgrade is one step closer to getting to the store.

The purpose of this is post is to share my re-discovery of the gift of Google (and other search engines). Just keep wording the question in different ways and settle in to read the Googleplex of characters that will form words and will flow into your device. I often have no idea what the author is writing about but have reading several articles on the same subject, the lights might come on!

Ok, my next hurdle: saving to Cloudkit and Sharing via Cloudkit. This is REALLY bewildering to me!

Happy coding and stay safe.

Are the colors that you want to save Dynamic or fixed?

Hey Chris,

Fixed I am guessing, UIColor.systemColorBlue, etc.

I did find an extension for UserDefaults that would change the color object into a Data object and then back into a Color object, but I am not at all familiar with UserDefaults, and I could not figure out the properties.

I ended up just saving the user selection as a String, saved that to Realm, and the when a ViewController populates, it grabs the String name of the color from Realm, checks an enum in the constants file grabs the UIColor and set the background property to the saved UIColor.

Took a while but I got there! :slight_smile:


I suppose the way you have done it will suffice.

Have you got a link to that extension you mentioned? I would be interested to see how they went about it.


Here you go Chris,

I would love to hear what you think. I really need to add UserDefaults to my toolbox!


UserDefaults are great!


Mark, I’ll have a look at that article and let you know. I’m familiar with UserDefaults so it shouldn’t take too much to figure out a solution.