Build your framework with the Swift Package Manager (SPM)

When we entered ViewController.swift we saw many Imports:

Screen Shot 2021-02-08 at 11 15 04

What are these imports:

They are called frameworks and are important to search for other classes, for example, in UIKit we can see several classes such as UIAlert, UIFont, UIButton.

Then you can create your framework. Explaining better, frameworks are called Modules and the classes they contain are called packages. Packages are a set of code (classes, attributes, methods, builders) that have the same hierarchy.

To make a framework we have a few options: Swift Package Manager, Cocoapods and Carthage, which are tools that manage packages.

Swift Package Manager (SPM)

It is a tool to manage the distribution of Swift code, which helps in easy sharing and use in other codes, in addition to that, Xcode has integration with SPM.

To create a framework you need to pay attention to:

  • Create a modular code.
  • Document the line of code.
  • You don't have to create something unique, or fantastic, you can create an extension of a class, a function.

We will create our framework, it will be an extension of UIColor, we will create a specific color of green.

Let's practice

Open the Xcode:

Screen Shot 2021-02-08 at 11 55 01

Let's create a framework:

Screen Shot 2021-02-08 at 12 13 32

Name something related to your project. My project will be called: CustomColorGreen

In the sources folder we will create everything related to the project, in CustomColorGreen.swift we will create our code:

Screen Shot 2021-02-08 at 12 14 57

We will import UIKit, because there is the package, UIColor, but first, we have to change our simulator for the iPhone, because the UIKit framework is only for iPhone and iPad and tvOS.

We created our UIColor extension, which can receive a modified green color.

Screen Shot 2021-02-08 at 14 52 44

import UIKit

@available(iOS 13.0, *)
extension UIColor{
    class func getCustomGreen() -> UIColor{
        return UIColor(displayP3Red: 180/255, green: 213/255, blue: 21/255, alpha: 1.0)
    }
}

How to add a framework to the project

First we need to push

First, we need to push to a repository, and then in a project, add the swift package manager.

Screen Shot 2021-02-08 at 15 34 14

After adding to Github, we will put in the project

We will create a new project and add:

Screen Shot 2021-02-08 at 15 40 04

We will copy the URL of the .git repository and place it in the text box. As our repository has no tag (version), then we will check the second option, to pull from the master:

Screen Shot 2021-02-08 at 15 43 38 Screen Shot 2021-02-08 at 15 43 56

Note that the project now has a framework:

Screen Shot 2021-02-08 at 15 44 42

Screen Shot 2021-02-08 at 15 45 14

Ready! We created our framework, we will use it in the view controller. I will paint the view with our modified UIColor.

Screen Shot 2021-02-08 at 15 47 27

As you can see, we didn't find our function, because we don't import our framework, we will import it! But we still can't find it, because we did two wrong things.

  • We don't document
  • We do not specify whether it is public or private.

To see how the framework is built, we can click command + click on the framework and go to the option: jump to definicion.

Screen Shot 2021-02-08 at 15 52 54

Screen Shot 2021-02-08 at 15 53 08

As you can see: it's blank.

Let's go back to our framework project

We put a public on our extension and we also document:

Screen Shot 2021-02-08 at 15 59 07

import UIKit

@available(iOS 13.0, *)
public extension UIColor{
    /// Create a modified green color
    /// - Returns: A modified green color
    class func getCustomGreen() -> UIColor{
        return UIColor(displayP3Red: 180/255, green: 213/255, blue: 21/255, alpha: 1.0)
    }
}

Let's push our branch framework

After pushing our branch, we have to update the framework that is in the project

Screen Shot 2021-02-08 at 16 01 59

Ready we update!

if we go to our ViewController and do a command + click on our import, we will see our function there

Screen Shot 2021-02-08 at 16 03 55

Now we can call our function in the ViewController, let's change the background color

Screen Shot 2021-02-08 at 16 05 06

Our framework is done!

Screen Shot 2021-02-08 at 19 20 35

Final considerations

Remember the framework can be made for various situations, at any level of complexity, either a circular progress view or a simple extension that is unique to your project. But the main thing about a framework, is the documentation and a modular code, because without them, other people will not understand anything.

The creation of a framework can go beyond: tests, CI / CD, IBDesignable and IBInspectable

The project is in this repository:

Github