Skip to main content

Enums in Swift

We can call enums as collection of similar type of values. 

What??

Below is a example of enum in swift. It's having a declaration name and inside the braces we will be writing different cases. 


Why do i need enums?

Firstly enums are type safe, once declared we can access the same cases everywhere in our project. Imagine you are trying to render a particular type of chart, In one view controller I will name type of a chart as barChart and in other barrChart (notice the typo, extra r) . Instead of doing this way we can have a enum storing all the supported chart types in our application. By this way we eliminate the duplication of code and it is  type safe. We can use a switch statement and check for specific case and render accordingly.



Comments

Popular posts from this blog

Async Await in Swift

 Async await came as a part of Swift 5.5 version. If you have a background in JavaScript then you might be aware of these two keywords. Use Case Let's say we are getting json in the below format. {   "ids": [     "av234",     "vasant"   ] } The above json is having, a parameter "ids" which contains the array of article id's. We will take the first id and pass to another service endpoint to get the article details. Below is the json from second service. {   "id": "av234",   "content": "Lorem ipsum",   "date": "03/05/2022",   "image_url": "https://google.com/some_image.png" }  Now we have to get the image by calling the "image_url" parameter. So totally we have to do three service calls. Using async await Using escaping closure Below is the code for service calls for escaping closure. I have written a class ApiCalls which is having three functions whic...