Framework Oriented Programming with Swift
Part 1, Motivation
Prior to iOS 8 and the launch of Swift, Objective-C libraries were integrated with your project as static frameworks (we could also add manually the files to our app target). These static frameworks included the symbolic links to the lib and the headers. The assets had to be distributed using .bundles. Dependency managers like CocoaPods read the .podspec file and created these static frameworks and bundles that were linked with our projects.
With the introduction of Swift, Xcode did not support creating static frameworks with Swift code. What was the solution proposed by Apple?Dynamic Frameworks. Dynamic Frameworks have long been a vital part of developing for MacOS, but Xcode did not support building them for iOS (Xcode refers to them as embedded frameworks)
Dynamic libraries allow an application to load code into its address space when it’s actually needed. This can be either at launch time or at runtime. Dynamic libraries are not part of the application executable file. When an application is launched, the application’s code is first loaded into the address space of the process. The dynamic loader — usually dyld on Apple’s platforms — takes control of the process and loads the dependent libraries. This involves resolving their location on the file system based on their install name, and then resolving the undefined external symbols needed by the application. The dynamic loader will also load additional libraries at runtime, as they are requested.
Why create Frameworks?
The main reason that motivates the creation of Frameworks when coding our apps is the reusability. Not only the libraries you find out there are reusable components, but the code inside your apps. If you write a bunch of code for the interaction with an API, that one might be reusable by another app that points to the same API. If you develop your own wrapper for CoreData, you might need that wrapper in the future. As an example, Apple tends to develop core frameworks for their main apps, HealthKit for their Health app, or MapKit for their Maps one.
If the code appears more than once, it probably belongs to a framework.
A very important motivation appeared when Apple released the Apple Watch, because we needed to reuse part of our code bases for it. I remember it was not easy when I tried, since the core components where very tied to each other. The design of the core architecture was not good enough and it resulted into a big headache. A few years ago we only had iPhone to develop for, iPad later, then Watch, and recently the Apple TV. If you start with a project thatmight be ported to multiple platforms, coding the core business logic in frameworks should be a must do.
Get GitDo’s stories in your inbox
Join Medium for free to get updates from this writer.
Implementing a framework we are actually creating an API that is going to be consumed from a another point. When we code everything in the same bundle we do not care that much about the interfaces, but the logic. When our frameworks are going to be consumed that is a very important point to be taken into account.
As developers, designing frameworks make us better developers and pay attention to aspects we didn’t pay attention before like visibility, documentation, testing, patterns, error handling, logging, …
We can find another big advantage in testing. Have you suffered vast code bases with more than hundreds of tests taking 5 minutes to be executed? I have. When you create frameworks you are also bundling tests in multiple testing targets. If anything changed from your framework, you don’t need to run the tests suite of your whole app but only the tests of your framework. It incredibly increases the speed of development. You can have people in your team focused on maintaining core frameworks, ensuring their logic is robust through unit tests, and providing you with the set of components your apps need for working.
In the next post we will analyze the important points to keep in mind before going ahead with your Framework: architecture ideas, dependency manager depending on your setup, versioning… Remember that you can subscribe to our mailing list and we’ll send new posts directly to your email when they get published.








