# Codable synthesis for enums \*without\* associated values

**URL:** <https://forums.swift.org/t/codable-synthesis-for-enums-without-associated-values/49044>\
**Category:** Using Swift\
**Created:** [May 30, 2021, 8:09am UTC](https://forums.swift.org/t/codable-synthesis-for-enums-without-associated-values/49044 "2021-05-30T08:09:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kansaichris](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kansaichris/32/2703_2.png) [@kansaichris](https://forums.swift.org/u/kansaichris)\
**Post date:** [May 30, 2021, 8:09am UTC](https://forums.swift.org/t/codable-synthesis-for-enums-without-associated-values/49044/1 "2021-05-30T08:09:05Z")

</div>

Assume we have a simple enumeration to represent the cardinal directions:

```
enum Direction {
    case N, S, E, W
}

```

Now let's say that we want to synthesize Codable conformance. I would have thought that the following would work:

```
enum Direction: Codable {
    case N, S, E, W
}

```

But Xcode 12.4 is telling me that I need to implement this conformance myself:

```
Type 'Direction' does not conform to protocol 'Decodable'
Type 'Direction' does not conform to protocol 'Encodable'

```

Does an enumeration like this need [raw values](https://docs.swift.org/swift-book/LanguageGuide/Enumerations.html#ID149)—either explicitly or [implicitly assigned](https://docs.swift.org/swift-book/LanguageGuide/Enumerations.html#ID535)—to qualify for automatic Codable conformance?

---

<div class="post-metadata">

**Author:** ![roosterboy](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/roosterboy/32/11706_2.png) [@roosterboy](https://forums.swift.org/u/roosterboy)\
**Post date:** [May 30, 2021, 8:29am UTC](https://forums.swift.org/t/codable-synthesis-for-enums-without-associated-values/49044/2 "2021-05-30T08:29:09Z")

</div>

> [@kansaichris](#):
>
> Does an enumeration like this need [raw values](https://docs.swift.org/swift-book/LanguageGuide/Enumerations.html#ID149)—either explicitly or [implicitly assigned](https://docs.swift.org/swift-book/LanguageGuide/Enumerations.html#ID535)—to qualify for automatic Codable conformance?

Yes, because otherwise what values will be put into the resulting JSON?

---

<div class="post-metadata">

**Author:** ![kansaichris](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kansaichris/32/2703_2.png) [@kansaichris](https://forums.swift.org/u/kansaichris)\
**Post date:** [May 30, 2021, 8:42am UTC](https://forums.swift.org/t/codable-synthesis-for-enums-without-associated-values/49044/3 "2021-05-30T08:42:03Z")

</div>

Note that the encoded result could be stored in a JSON file, a property list, or some other format, but in any case I honestly wasn't sure what values would be generated. I guess I just assumed that each case would be assigned an integer or string value simply for the purpose of Codable conformance after finding a number of [highly-ranked Google search results that seem to imply this is possible](https://lostmoa.com/blog/CodableConformanceForSwiftEnumsWithMultipleAssociatedValuesOfDifferentTypes/):

> As of Swift 5 only enums without associated values have automatic conformance to Codable. For example, to make our `EditSubview` conform to Codable we only need to indicate the conformance in the declaration.
> 
> ```swift
> enum EditSubview: Codable {
> case headers
> case query
> case body
> }
> 
> ```

So is it safe to assume that the information in these blog posts is inaccurate?

---

<div class="post-metadata">

**Author:** ![roosterboy](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/roosterboy/32/11706_2.png) [@roosterboy](https://forums.swift.org/u/roosterboy)\
**Post date:** [May 30, 2021, 8:47am UTC](https://forums.swift.org/t/codable-synthesis-for-enums-without-associated-values/49044/4 "2021-05-30T08:47:11Z")

</div>

Yes, that code won't compile. Note that if you follow the link in that blog post to its corresponding GitHub repository, the `EditSubview` enum looks like this:

```swift
enum EditSubview: String, Codable {
    case headers
    case query
    case body
}

```

which does compile since it has a raw type that conforms to `Codable`.

So I think that's probably just an oversight or editing error or something like that in the blog post.

---

<div class="post-metadata">

**Author:** ![kansaichris](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kansaichris/32/2703_2.png) [@kansaichris](https://forums.swift.org/u/kansaichris)\
**Post date:** [May 30, 2021, 8:48am UTC](https://forums.swift.org/t/codable-synthesis-for-enums-without-associated-values/49044/5 "2021-05-30T08:48:41Z")

</div>

Ahhh I see—thanks for clarifying! Good to know that I wasn't missing something here. 👍
