Swift | Multi cases Enum handling
Best way to handle swift Enum with multi cases
Enums are powerful thing in Swift as compared with Objective-c. We can do many things with Enum. I hope you know basics of enum if not please refer apple doc, because i’m not covering those in this article, here we see how we write Enum which have multiple cases.
You think that, this is so simple just create Enum and add cases which we want in that. Right?
So, let’s see what is issue with multiple cases inside Enum.
Mostly, we are using Switch block for identify enum types and switch is treated as normal if_else block means how many cases we write inside switch all those we can consider as if _ else. So let's consider we have our custom color/ font or other Enum with have more than 15–20 cases. (In application there are many cases where we declare more than 15 cases inside Enum). If we handle all those 20 cases in switch block and if you using SwiftLint then mostly you will following warnings.
function body length
cyclomatic complexity
For example we take colors, where we declared all our standard colors inside single Enum as below:

Here, we have standard four colors with 5 types (extraLight, light, standard, dark and extraDark) and getColor func will return UIColor as per type.
For get color from this Enum, we write function inside UIColor extension as below:

We used it as below:

So, there are 3 ways for get color from this enum. using static func, enum type called getColor or set rawValue for cases and get Color.
So in above code swiftLint should give warnings as cyclomatic_complexity and function_body_length. So how we solve this issue using Enum only. I know some developers use direct static variables for colors inside UIColors but take this as example of multiple cases Enum.
When ever we tried to get color from it using getColor function that time inside switch will call and max iteration count will number of cases. Here we can avoid it using rawValue but again in that one issue as if we need some other things from Enum then we can’t declare multiple rawValue so in that case we need to write func inside enum.
Solution!!
As Swift called as Protocol oriented language and as per that we should write protocol before writing anything (Class, Struct, Enum etc.), so we follow this for our problem.
A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements. Any type that satisfies the requirements of a protocol is said to conform to that protocol.
In addition to specifying requirements that conforming types must implement, you can extend a protocol to implement some of these requirements or to implement additional functionality that conforming types can take advantage of.
Right now we need one protocol and it should have one func called as getColor which return the UIColor.

As you know Swift Enum can adopt Protocol as like Class and struct, so we will create our Color Enum now. but wait we will not create Enum as like above instead of that we separate our color and every color should have own enum and inside that we will declare color types (light, dark etc)


In above code, we have created separate Enum for each color called as StandardPurple, StandardRed, StandardGray and StandardYellow. Inside all colors have same cases. All enum’s adopt StandardColorProtocol so we implemented getColor() method in all Enum.
Now, we need to access it. for that we write one static func inside UIColor extension as below:

This function is almost similar with previous one, but if you parameter type is changes in previous func it declared as Enum type but now it is Protocol type. It means we can pass any Enum cases here which adopt StandardColorProtocol as below:

So, use of it almost same as like previous one and have 3 ways for get color from Enum.
Using protocol, we can avoid multiline and multiple condition code easily. So before writing any Enum try this practice.
If you have any questions or comments, feel free to comment below.
Happy Coding!!!


Comments
There are no comments for this story
Be the first to respond and start the conversation.