While preparing for my talk for mDevCamp 2016, I was working on a new project interfacing with a RESTful web service. This allowed me to design the API without having to consider compatibility with Objective-C. As a result I could employ several advanced Swift techniques, and in this talk I am explaining these.
When starting with Swift, I consider all those techniques “High Hanging Fruit” which have no representation in Objective-C. As such you could not employ these if you want your API to be usable from Obj-C code.
I am trying to draw the parallel with the Theory of Evolution, where stretching for the higher branches supposedly caused giraffes to evolve longer necks and legs due to those making them fitter for survival. By analog, I propose that we developers should reach for advanced Swift techniques whenever we can, as to preserve our long term advantage.
By combining associated values in enumerations with generics and ErrorType-based error handling you naturally end up with a simpler and more concise solution for the presented use case. And the overall result being way more delightful for me as a developer then having to stick with prior “best practises”.
When carrying through this thought process, I naturally gravitated to a similar solution as Rob Rix had invented in 2015: the Result framework. This was invented before Apple introduced try/catch error handling in Swift 2 as a way to model the success/failure result of calling a web service function.
Although I prefer working with try/catch for synchronous code, the Result-oriented model remains a perfect fit for asynchronous handing of web service requests. This, as well as the joy of figuring out how the pieces fit together informed this talk.
If you have the luxury of not having to consider ObjC compatibility, then you can open your mind to those fascinating Swift language capabilities.
The talk was also recorded by SlidesLive.
Also published on Medium.