# Adding Realm as a dependency to an iOS Framework

# Adding Realm as a Dependency to an iOS Framework

## Introduction

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.

## The Problem 

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 ~~no code~~ 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?

## Solution 1: Use Xcode to Create the Framework and Add Realm with SPM

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. 

In this case, we’ll add Realm Cocoa, a package that contains two libraries. We’re interested in  Realm Swift: https://github.com/realm/realm-cocoa. We want one of the latest versions, so we’ll choose “Up to major version” 10.0.0. Once the resolution process is done, we can select RealmSwift.

Nice! Now that the package is added to our Framework we can compile our code containing Realm Objects without any problems!

## Solution 2: create the Framework using SPM and add the dependency directly in Package.swift

The other way to author a framework is to create it using the Swift Package Manager. We need to add a Package Manifest (the Package.swift file), and follow a certain folder structure. We have two options here to create the package:

*   Use the Terminal
*   Use Xcode

### Creating the Package from Terminal

*   Open Terminal / CLI
*   Create a folder with `mkdir yourframeworkname`
*   Enter that folder with `cd yourframeworkname`
*   Run `swift package init`
*   Once created, you can open the package with `open Package.swift`

    

### Creating the Package using Xcode

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.

### Adding Realm as a dependency

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 `Package.swift`

The complete `Package.swift` file

```swift
let package = Package(
    name: "BinaryTree",
    platforms: 
        .iOS(.v14)
    ],
    products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(
            name: "BinaryTree",
            targets: ["BinaryTree"]),
    ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        .package(name: "Realm", url: "https://github.com/realm/realm-cocoa", from: "10.7.0")
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages this package depends on.
        .target(
            name: "BinaryTree",
            dependencies: [.product(name: "RealmSwift", package: "Realm")]),
        .testTarget(
            name: "BinaryTreeTests",
            dependencies: ["BinaryTree"]),
    ]
)
```

Here, we declare a package named “BinaryTree”, supporting iOS 14

```swift
let package = Package(
    name: "BinaryTree",
    platforms: [
        .iOS(.v14)
    ],
```

As this is a library, we declare the products we’re going to build, in this case it’s just one target called `BinaryTree`.

```swift
products: [
        // Products define the executables and libraries a package produces, and make them visible to other packages.
        .library(
            name: "BinaryTree",
            targets: ["BinaryTree"]),
    ],
```

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.

```swift
dependencies: [
        // Dependencies declare other packages that this package depends on.
        .package(name: "Realm", url: "https://github.com/realm/realm-cocoa", from: "10.7.0")
    ],
```

In our target, we use the previously defined `Realm` dependency.

```swift
.target(
            name: "BinaryTree",
            dependencies: [.product(name: "RealmSwift", package: "Realm")]),
```

And that’s all! Now our library can be used as a Swift Package normally, and it will include automatically Realm.

## Recap

In this post we’ve seen different ways to create a Framework, directly from Xcode or as a Swift Package, and how to add `Realm` as 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!

If you have questions, please head to our [developer community website where the Realm engineers and the Realm/MongoDB community will help you build your next big idea with Realm and MongoDB.