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:
Comments
0 comments
Please sign in to leave a comment.