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…

Image for post
Image for post
class Celsius {var temperatureInCelsius: Doubleinit(fromFahrenheit fahrenheit: Double) {temperatureInCelsius = (fahrenheit — 32.0) / 1.8}init(fromKelvin kelvin: Double) {temperatureInCelsius = kelvin — 273.15}func printTemperature() {print(“Temperature is \(temperatureInCelsius)”)}}
Celsius(fromKelvin: 400).printTemperature() 
// prints Temperature is 126.85

Chaining methods example:

let params = [“key1”:”value1",”key2":”value2"]APICaller().urlString(“www.google.com").method(.post).parameters(params).response { (resultDict) inprint(resultDict[“result”]!)}//----------------
// prints the following in console.
("Result values1","Result values2","Result values3","Result values4")

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