Generic UserDefaults wrapper

Sayler8182
Sep 7, 2018 · 1 min read

In my previous article I described how to create generic class for storage Realm objects. But what if we don’t want to use such advanced libraries and keep data in UserDefaults. We could also use generic class. It sounds so cool.


At the beginning, like usual, we must create our protocol

And implement it for our storage class

And that’s it. Now we could read or write not only primitives but also any class conforms to a Storage protocol. You want some example? Below you have more complex object.

And here you have full example


Thank you for reading! If you liked this article, please clap so other people can read it too. I will also have more motivation for next article :) You can also see my other articles, maybe you will find something for you.

If you have any question or suggestion leave a comment.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade