Adding Realm as a dependency to an iOS Framework
Rate this tutorial
In this post we’ll review how we can add RealmSwift as a dependency to our libraries, using two different methods: Xcode assistants and the Swift Package Manager.
I have a little, nice Binary Tree library. I know that I will use it for a later project, and that it'll be used at least in a macOS and iOS app. Maybe also a Vapor web app. So I decided to create a Framework to hold this code. But some of my model classes there need to be persisted in some way locally in the phone later. The Realm library is perfect for this, as I can start working with regular objects and store them locally and, later, if I need a really simple & quick to implement backend solution I can use Atlas Device Sync.
But the problem is, how do we add Realm as a dependency in our Frameworks?
The first way to create the Framework is just to create a new Xcode Project. Start Xcode and select
File > New > Project. In this case I’ll change to the iOS tab, scroll down to the Framework & Library section, then select Framework. This way I can share this Framework between my iOS app and its extensions, for instance.
Now we have a new project that holds our code. This project has two targets, one to build the Framework itself and a second one to run our Unit Tests. Every time we write code we should test it, but this is especially important for reusable code, as one bug can propagate to multiple places.
To add Realm/Swift as a dependency, open your project file in the File Navigator. Then click on the Project Name and change to the Swift Packages tab. Finally click on the + button to add a new package.
Nice! Now that the package is added to our Framework we can compile our code containing Realm Objects without any problems!
- Use the Terminal
- Use Xcode
- Open Terminal / CLI
- Create a folder with
- Enter that folder with
swift package init
- Once created, you can open the package with
You can also use Xcode to do all this for you. Just go to
File > New > Swift Package, give it a name and you’ll get your package with the same structure.
So we have our Framework, with our library code and we can distribute it easily using Swift Package Manager. Now, we need to add Realm Swift. We don’t have the nice assistant that Xcode shows when you create the Framework using Xcode, so we need to add it manually to
Here, we declare a package named “BinaryTree”, supporting iOS 14
As this is a library, we declare the products we’re going to build, in this case it’s just one target called
Now, the important part: we declare Realm as a dependency in our library. We’re giving this dependency the short name “Realm” so we can refer to it in the next step.
In our target, we use the previously defined
And that’s all! Now our library can be used as a Swift Package normally, and it will include automatically Realm.
In this post we’ve seen different ways to create a Framework, directly from Xcode or as a Swift Package, and how to add
Realmas a dependency to that Framework. This way, we can write code that uses Realm and distribute it quickly using SPM.
In our next post in this series we’ll document this library using the new Documentation Compiler (DocC) from Apple. Stay tuned and thanks for reading!