How To Create A Framework In Swift

A framework is a bundle which contains a library along with associated resources like image files, nib files, header files,etc. It helps you to reduce your work as it already contains some functions within it which you can directly use in your code.
 
In this article, I will explain how to create a framework in Swift and also I will build a simple application of adding two numbers using this framework. So let's begin!
 
Prerequisites
  1. MacOS
  2. Xcode
Open Xcode and select iOS -> Framework and Library -> Framework. Click Next.
Note
In some Xcodes, you will find the option as Cocoa Touch Framework.

How To Create A Framework In Swift
 
Name your Product and select the language as Swift. Click Next. Save the project in your desired location and click Create.
 
How To Create A Framework In Swift
 
Now, we will add one more file to our Project. Right-click on your Project present on the left-hand side with the blue icon  and select Add -> New File. Alteratively you can add a new file by going to File -> New File. Select a Swift file as shown below. Click Next.
 
How To Create A Framework In Swift
 
Name your file and click Create. I have named my file as 'SumOfTwoNumbers'. Now you can see that the Swift file is added into our Project.
 
How To Create A Framework In Swift
 
Copy and paste the following code into the Swift file. Make sure you use the keywords like 'public' or 'open' to the functions and classes otherwise they won't work when you import the framework in other applications.
  1. import Foundation  
  2. public func SumOfTwoNumbers(num1: Int, num2: Int)-> Int{  
  3.    return num1 + num2  
  4.  }  
Run the Project and ensure it succeeds. Right-click on your .framework file created under the Products folder and click on 'Show in Finder'. The Finder will show you the framework that has been created.
Note
In the below image, you can see the framework is created in the Debug-iphonesimulator folder. This is because I have run my project using the simulator option. If you have run the project using a real iPhone device or by selecting the option as GenericOS Device, the framework will be created in the Debug-iphoneos folder.

How To Create A Framework In Swift
 
Congrats! We have successfully created the framework!
 
Now let's use the newly-created framework in an application. As you can see, I have created a framework which will add two numbers, so let's create an application for the addition of two numbers.
 
Create a new project using Single View App. Design a UI on the Main.storyboard.  In the below image, you can see  I have created a UI with two text boxes, a button which will perform the addition function, and a text box to display the result.
 
How To Create A Framework In Swift
 
Connect all the tools used (i.e TextBoxes, Button,Labels) to the ViewController.swift file using the assistant editor.
How To Create A Framework In Swift
 
Now, go back to your previous Framework project and copy the framework that has been created in the 'Debug-iphonesimulator/Debug-iphoneos' folder. Follow the below steps shown in the images.
How To Create A Framework In Swift

How To Create A Framework In Swift
 
Open your current project. Right-click on your Project with the blue icon present on the left-hand side and click 'Show in Finder'. Paste the copied framework inside your current project directory. Also, add the framework in 'Framework,Libraries and Embedded Content'.

How To Create A Framework In Swift
 
Copy and paste the code for addition operation inside the Add button in the ViewController.swift file. Check if the functions you used inside the framework appear here. Refer to the code below.
 
Note
Do not forget to add 'import AddTwoNumbers(name of the framework)' inside the files you work with.
  1. import UIKit  
  2. import AddTwoNumbers  
  3. class ViewController: UIViewController {  
  4.     @IBOutlet weak  
  5.     var lblNum1: UILabel!@IBOutlet weak  
  6.     var lblResult: UILabel!@IBOutlet weak  
  7.     var lblNum2: UILabel!@IBOutlet weak  
  8.     var txtResult: UITextField!@IBOutlet weak  
  9.     var txtNum2: UITextField!@IBOutlet weak  
  10.     var txtNum1: UITextField!@IBAction func btnAdd(_ sender: Any) {  
  11.         let no1 = Int(txtNum1.text!)  
  12.         let no2 = Int(txtNum2.text!)  
  13.         let result = Int(SumOfTwoNumbers(num1: no1!, num2: no2!))  
  14.         txtResult.text = "\(result)"  
  15.     }  
  16.     override func viewDidLoad() {  
  17.         super.viewDidLoad()  
  18.         // Do any additional setup after loading the view.  
  19.     }  
  20. }  
Run the application and ensure it succeeds.
 
Run and check if the application executes properly in the simulator/device.

How To Create A Framework In Swift
 
 Some important tips
  • Check if you have added 'public' or 'open' keywords to the functions and classes you have created inside your framework.
  • Ensure you have run the framework project and it succeeds before importing it into another project.
  • Ensure you have imported the framework properly inside your application according to the steps explained above.
  • Check if you have added the line 'import (FrameworkName)' in all the files where you need to use the framework.

Similar Articles