Nested Enums and Protocol Extensions for Better URLs

When consuming a web service in iOS we all have seen similar code as shown below:

The above code will work fine but it might turn into a maintenance nightmare when the base URL changes or the service URL changes. Let’s see how we can improve URLs using nested enums and protocol extensions.

Instead of using String URLs I have used an enum to define the structure of paths associated with the URLs.

Now, I can use the App enum instead of the String type as shown below:

I can even pass parameters as shown below:

I think using nested enums gives more flexibility in creating the overall structure of the URLs and it also provides strong type support when using URLs in codebase.

Show your support

Clapping shows how much you appreciated Mohammad Azam’s story.