Laurence Wingo
Laurence Wingo

Reputation: 3952

Is this enum of two different types?

I'm following a tutorial and I noticed the author(s) declared this enum with what looks like to be multiple types. Based on what I've read online from the Swift Standard Library, I understand enums can be of a certain type and enums do not support inheritance. Is this enum of both String and CodingKey type? Or is the name case a String type and the items case a CodingKey type?

private enum CodingKeys: String, CodingKey {
        case name
        case items
    }

Upvotes: 0

Views: 251

Answers (1)

Alexander
Alexander

Reputation: 63167

This isn't inheritance, it's two things:

  1. A raw value clause. This is a special form that speficies the "backing value" used to represent the enum's cases. In this case, it's String. When an enum has chosen to have a String raw value, but the case doesn't specify a raw value, the name of the case is implicitly assumed to be the raw value of the case.
  2. A protocol conformance clause, which declares that this enum conforms to CodingKey.

Upvotes: 3

Related Questions