---
title: Codable enums in Swift
description: Get to know how to implement the Codable protocol for most popular enums use cases.
image: https://www.untitledkingdom.com/hubfs/Untitled_Kingdom_February2019/Images/1_kQ3cEpQGCWoe0wERrl2Tqw.jpeg
---

[Skip to content](https://www.untitledkingdom.com/blog/codable-enums-in-swift#main-content)

[Untitled Kingdom](https://www.untitledkingdom.com)

Enable/disable contrast

small font

medium font

large font

Menu

- [Projects](https://www.untitledkingdom.com/projects)
- [Careers](https://www.untitledkingdom.com/careers)
  
  Toggle Careers submenu 
  
    - [Culture](https://www.untitledkingdom.com/careers#culture)
    - [Academy](https://www.untitledkingdom.com/academy-kingdom)
- [UK process & services](https://www.untitledkingdom.com/services)

[Contact](https://cta-redirect.hubspot.com/cta/redirect/5448061/e97f83e4-5d50-4d26-af42-d4a0f0a2c1ef)

![](https://cdn2.hubspot.net/hubfs/5448061/Untitled_Kingdom_February2019/Images/1_kQ3cEpQGCWoe0wERrl2Tqw.jpeg)

# Codable enums in Swift

![Grzegorz Przybyła](https://cdn2.hubspot.net/hub/5448061/hubfs/gregios_small.jpg?width=150&length=150&name=gregios_small.jpg)

 By [Grzegorz Przybyła](https://www.untitledkingdom.com/blog/author/grzegorz-przybyla),  on January 31, 2018 

 2 min read

- [Share on Facebook](https://www.facebook.com/sharer.php?u=https://www.untitledkingdom.com/blog/codable-enums-in-swift)
- [Share on Twitter](https://twitter.com/intent/tweet?url=https://www.untitledkingdom.com/blog/codable-enums-in-swift&text=Codable%20enums%20in Swift)
- [Share on LinkedIn](https://www.linkedin.com/shareArticle?mini=true&url=https://www.untitledkingdom.com/blog/codable-enums-in-swift&title=Codable%20enums%20in Swift&summary=Swift%20Enums%20are%20a%20powerful%20tool,%20so%20a%20lot%20of%20iOS%20developers%20are%20using%20those%20on%20a%20daily%20basis.%20Today...)
- [Share on Mail](mailto:?subject=Codable%20enums%20in Swift&body=)

 2 min

Swift Enums are a powerful tool, so a lot of[iOS developers](https://www.untitledkingdom.com/services) are using those on a daily basis. Today I would like to show how to implement the Codable protocol for most popular enums use cases.

---

Let’s start with simple, old-fashioned enum:

Simple enum codable

To make this enum codable we need to do nothing but to conform the `Codable` protocol. Codable is a protocol composition typealias:

`typealias Codable = Encodable & Decodable`

You can ask why I didn’t use the rawValued enum here. There is a simple answer; I would like not to make public (as rawValue) the implementation details of this protocol methods.

![Codable enums in Swift](https://www.untitledkingdom.com/hubfs/Untitled_Kingdom_February2019/Images/1_kQ3cEpQGCWoe0wERrl2Tqw.jpeg)

source: unsplash.com

## Going further

The real beauty and straight of the enums come with associated Type enums. Let’s check if it is possible to make this type of enums `Codable`.

Conform to the Codable protocol:

But can we handle the Optional associated values? The answer is YES, but we need to make one small modification.

## Enum with associated optional codable

All we need to do is to make a different method while decoding the associated values: `decodeIfPresent`, which is very useful when working with optionals for Codable protocols.

Maybe you are wondering what was coded under the associtatedValue key for the nil value, let’s check in Swift repo:

So using the JSONEncoder under the `associatedValue,` we will get `null` saved, which you can check by getting the `Codable.playground` in my repo:

[https://github.com/gregiOS/Playgrounds/](https://github.com/gregiOS/Playgrounds/)

Thanks for reading! 🚀

---

 

**Check out available job opportunities at Untitled Kingdom:**  
[https://www.untitledkingdom.com/careers](https://www.untitledkingdom.com/careers)

[Software Development](https://www.untitledkingdom.com/blog/tag/software-development) [Knowledgebase](https://www.untitledkingdom.com/blog/tag/knowledgebase) [Digital Product Development](https://www.untitledkingdom.com/blog/tag/digital-product-development) [Healthy job](https://www.untitledkingdom.com/blog/tag/healthy-job)

## 📈🩺 Looking for more articles about developing digital health solutions?

 Join the Untitled Kingdom newsletter. Sent on the 20th day of the month. Once a month. Industry news, scientific research & any information useful for developing MedTech.

![Grzegorz Przybyła](https://cdn2.hubspot.net/hub/5448061/hubfs/gregios_small.jpg?width=480&length=480&name=gregios_small.jpg)

- [E-mail](mailto:hi@untitledkingdom.com)

## By [Grzegorz Przybyła](https://www.untitledkingdom.com/blog/author/grzegorz-przybyla)

 iOS Developer. On Untitled Kingdom blog writing about all things Swift and communication-related.

- [Newsletter terms of service](https://www.untitledkingdom.com/newsletter-terms-of-service)
- [Privacy policy & cookies](https://www.untitledkingdom.com/privacy-policy)

Untitled Kingdom Sp. z o.o.

Przemysłowa 12, 30-701 Kraków

Poland 

Contact [ hi@untitledkingdom.com](mailto:hi@untitledkingdom.com)

- [LinkedIn](https://www.linkedin.com/company/untitledkingdom)
- [Facebook](https://www.facebook.com/untitledkingdom)
- [Instagram](https://www.instagram.com/untitled_kingdom)
- [Twitter](https://twitter.com/untitledkingd)

- [Ebooks & Resources](https://www.untitledkingdom.com/resources)
- [Blog](https://www.untitledkingdom.com/blog)
- [Press Room](https://www.untitledkingdom.com/press-room)

© 2022 Copyright ![Untitled.kingd](//5448061.fs1.hubspotusercontent-na1.net/hubfs/5448061/raw_assets/public/uk-22/_/img/logo-footer.svg)