Robert Atkins Robert Atkins - 4 years ago 92
Swift Question

Can Swift enums have multiple raw values?

I want to associate two raw values to an enum instance (imagine an enum representing error types, I want

to have an Int type property
with value 418, and a String property set to
I'm a teapot

Note the difference between raw values and associated values here—I want all
instances to have a
of 418, I don't want a unique associated value for each

Is there a better way than adding computed properties to the enum that
ed on
to look up the appropriate value?

Answer Source

No, an enum cannot have multiple raw values - it has to be a single value, implementing the Equatable protocol, and be literal-convertible as described in the documentation.

I think the best approach in your case is to use the error code as raw value, and a property backed by a prepopulated static dictionary with the error code as key and the text as value.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download