Build your framework with the Swift Package Manager (SPM)
When we entered ViewController.swift we saw many Imports:
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:
Let's create a framework:
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:
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.
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.
After adding to Github, we will put in the project
We will create a new project and add:
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:
Note that the project now has a framework:
Ready! We created our framework, we will use it in the view controller. I will paint the view with our modified UIColor.
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.
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:
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
Ready we update!
if we go to our ViewController and do a command + click on our import, we will see our function there
Now we can call our function in the ViewController, let's change the background color
Our framework is done!
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: