Swift 5 Future Enum Cases

Md. Ibrahim Hassan
Apr 1 · 3 min read

Traditionally in Objective-C and C, the enumerated type is simply a specific list of integers, where each value is given a name. By default, the first element in a list is set equal to zero, and each subsequent element is assigned a value one greater.

Enumerations in Swift are much more flexible. If the values for all enum cases are provided they can be a string, character, any integer or floating point type. Enums can have computed properties, initializers, instance functions and can also conform to protocols.

Now, there are two types of enums ie. frozen and non-frozen enums. A non-frozen enum is an enum to which new enumeration cases can be added in the future and any code which interacts with that enumeration should be able to handle the future cases without being recompiled.

The user-defined Swift enums are considered frozen. Only the standard library, Swift overlays for Apple frameworks, C and Objective-C code can declare non-frozen enums.

When switching over a non-frozen enum we should add a default case even if every case of the enum has a corresponding switch case, to handle any future enum cases added in the future.

Handling unknown case using @unknown default:

Like the regular default, @unknown default matches any value; it is a "catch-all" case. However, the compiler will produce a warning if all known elements of the enum have not already been matched. This is a warning rather than an error so that adding new elements to the enum remains a source-compatible change.

Handling unknown case using @unknown case _:

All enums declared in Objective-C as NS_ENUM are considered non-frozen in Swift and enum declared as NS_CLOSED_ENUM are considered frozen. We can also alter the frozen and non-frozen state of NS_ENUM by the newly added C-attribute enum_extensibility.

The states forenum_extensibility are enum_extensibility(closed) or enum_extensibility(open).

Conclusion

When switching over a non-frozen enum in Swift 5, the compiler gives a warning that there may be unknown values in the future. This warning can be silenced by adding a default case, @unknown default or @unknown case _: .

If we use default:, while switching over a non-frozen enum we can possibly miss handling some of the enum cases and the compiler would not throw a warning.

If we use @unknown default or @unknown case _: while switching over a non-frozen enum and miss handling any enum cases then the compiler throws a warning that the switch must be exhaustive.

References:

  1. https://github.com/apple/swift-evolution/blob/master/proposals/0192-non-exhaustive-enums.md
  2. https://www.swiftjectivec.com/ns_closed_enum/
  3. https://blog.udemy.com/objective-c-enum/

You can contact us on Twitter, Linkedin, Facebook, and Github.

Swiftify

Swiftify.com

Thanks to Alex Petuschak

Md. Ibrahim Hassan

Written by

 Developer | Tech Enthusiast

Swiftify

Swiftify

Swiftify.com

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