How to Use Inversion of Control in an iOS App

By Rajiv Ramnath

Frameworks for iOS apps often implement a technique known as inversion of control. As you already know, a software framework is a set of components containing common code that provides generic functionality. This generic functionality can be selectively overridden or specialized by user code. In this manner, the framework can be customized into providing specific functionality and flow of control also referred to as inversion of control.

Inversion of control means that the program’s flow of control — how the program runs — isn’t dictated by the user of the framework but by the framework itself, as default behavior. This default behavior is extended or modified by code that the user supplies in the form of classes that extend the base classes of the framework (which are usually abstract classes).

These user-supplied classes either override certain methods or provide implementations of virtual methods. The user’s classes may also implement specific interfaces that are then passed to and invoked by the framework. Inversion of control distinguishes frameworks from software libraries that provide computational capabilities that your application can invoke, such as the string manipulation routines provided by the java.lang package.

iOS is, in fact, a framework and it provides a set of base classes that you extend with your own classes and code and an overall sequence of computation in which you plug in these classes and your code. Separating out the model, views, and controller results in components with higher cohesion, with each component responsible for a related set of responsibilities.

Also, in order for inversion of control to work well, many different kinds of reusable components must be able to plug into the overall sequence of computation. The more these different components need to know about the framework, and vice versa, the harder it becomes to plug in a varied set of components. Consequently, loose coupling is also essential in frameworks.