What is a module?
In Xcode we have the possibility to create modules like in other IDE/programming languages. Maybe you are aware of dlls in C++, or jar file in Java. These are self contained pieces of program (compiled or not) which are usually responsible for a main concept. You can imagine a module as a group of classes / struct / protocols/ code which would follow the Single Responsibility Principle at a higher level.
If you want your application to scale well, I recommend having your code split into modules. It can be just a few to hundred of modules. There are no limitations.
When I worked in France in 2006 , we had our main application split into more than 150 dlls. This makes the dependencies and responsibilities clearer. We also had each of the members of the team responsible for a set of modules, owning them, maintaining them. Also it was really easy to recompose the dlls differently and create other version of our application. Think in iOS how it would be great to have widgets, extensions, app clips now with iOS14, using only what they need and not depending on the full code base of the app.
Modules in Swift
A module is a single unit of code distribution-a framework or application that is built and shipped as a single unit and that can be imported by another module with Swift’s import keyword
In Swift we have mostly 3 kinds of modules and they all have their specificities.
- Contains code only
- Linked at compile time with the static linker
- Can be “faster”
- When selecting, we don’t embed, code will be part of the executable
Framework / Dynamic Library
- Can contain code and resources (images, Xib, Storyboard…)
- If you want to have Swift UI, you need Dynamic Library for now, it’s not working with Static Lib
- Dynamic library are linked at runtime using dynamic linker
- When selecting, we usually embed the library in the executable.
- If not embedded, and the lib is not there, the app can crash at run time
- Can be distributed without source code
- This is the most common used module
- You have it automatically if you start creating an application for iOS or tvOS
- Very often will contain all the code of the app (unfortunately)
Let’s create some module in Xcode
- Select your project, then click on the + on the list in the bottom
- Select your preferred platform, but it doesn’t matter for now. Then select “Framework”, then give it a name
- You can see that Xcode creates a module package, and a test bundle automatically for you. Therefore we’ll be able to add code in the module and run the unit tests only for that module individually
- If you select “Build Settings”, you are able to specify the type of the module, by searching for “Mach-O Type” and selecting Static Library or “Dynamic Library“
- Then you need to specify on which platform your Module can be used. You may like to have a lib which you can reuse on iOS, tvOS, MacOS or even Watch os. Therefore you are specify which platform the module supports. For setting this, search for “Supported Platforms” in Build Settings
- iphoneos, iphonesimulator for iOS
- appletvos, appletvsimulator for tvOS
- macosx for MacOS
- Then, if you want to use your library in your executable, select it from General / Framework, Libraries to add
- Click + then select your framework
- If it’s a static Library, choose -> Do not embed
- If it’s a Dynamic Library, choose -> Embed and Sign
Building an application using module, will makes heavy usage of Access control. That means you can hide some code you don’t want to expose to be only accessible to your library. On the contrary, code which you want to be accessible will need to be marked as public or open, so it becomes visible from outside.
If you need to know more, you can check the Swift documentation around access level.
Thank you for reading until here… 👊 Have a good week end… 🍺