Chaining methods in swift — (Don’t confuse with optional chaining)

Last day, when I was doing the api calls using almofire, I came across a weird looking method call which was something like creating an object of almofire and then calling a method with the object created ,followed by another method after a dot(.), followed by another method so far and so on…

The api call I was talking looks something like this:

Here , we are calling a method called request on the Almofire class. This is followed by a method called validate(statusCode:), then another method called validate(contentType:) . Then at last we can see a method called responseData with a trailing closure which returns the output of the api call.

How does it work?? Well, the logic is quite simple.

Making functions chainable is quite easy and can allow us to write using an almost DSL-like syntax.

We’ll add a new function that does something and then return self. It's that simple.

Let’s make it more clear. we normally have an init method for initializing an object of a class. We can have multiple designated as well as convenience initializers to make our job easier. We can have custom parameters for initializing the class object . Consider a class called Celcius .

We have two init methods, one accepting temperature in kelvin, other one in fahrenheit. The following syntax creates an object of class Celcius and using that object , we can call an instance method.

If you are aware how the above code worked, then understanding the almofire method chaining mentioned in the beginning of the article is easy to understand.

Chaining methods example:

Let’s create an APICaller class.

An api call normally have a url, an http method and parameters . If we use the above class to make an API call, it will look something like this.

Let’s split up the above call.

  1. Create an instance of APICaller class.
  2. Call urlString method with a string as parameter. This will set the url property in APICaller class and return self .
  3. Call method method which set the method property of APICaller class and return self .
  4. Call parameters method which sets the parameters for the api call and return self .
  5. Finally, call the method called response which has a trailing closure as parameter. Here, do the API call stuff and on success or failure, call the callback closure with the result object. Here the closure takes only a dictionary of type Dictionary<String,AnyObject>.

I hope everything is clear. Either we can have a single init method with all these parameters and call a method which in turn calls the api and return the response. But, here the greatest advantage is that we need to call any chaining method only when required. If we are doing a get method call, we can skip the method method call as get is given as the default method inside the code. Also if parameters are not required, we may skip those too.

Our objective is to write less code which makes more sense!!!


If you enjoyed reading this post, please share and recommend it so others can find it 💚💚💚💚💚💚 !!!!

iOS and tvOS developer, dreamer, photographer 🤨

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store