Creating Swift Packages

Creating Swift Packages

Let us create our first Swift Package !

iOS Developer level : Intermediate

Configuration when this article was written

Xcode : Version 11.5

MacOS : 10.15.5 (Catalina)

A working example

Final code can be found in the Resources section below.

What are Swift Packages ?

Swift packages are reusable components of Swift, Objective-C, Objective-C++, C, or C++ code that developers can use in their projects. They bundle source files, binaries, and resources in a way that’s easy to use in your app’s project.

Enough of introduction , let’s be technical…

From Xcode 11, we can directly create Swift Package.

To ensure our package is rightly developed we will follow the steps mentioned below.

Step 1 : We create a single view application , we will have a service (which we later export as swift package) developed here

Step 2 : Create Swift Package with the same single view application just developed, Move our service developed from our app to package.

Step 3 : Once we find our service working. Lets host this swift package on GitHub and Tag it.

Step 4 : Create a new single view application from xcode and add Swift Package dependency from Remote (Github where we hosted.)

This is all we need to do.. So let us get steps rolling...

Step 1 : Create our service with a single view app

Go to get Xcode and create a single view application and name it as MySwiftPackageCreator

Xcode -> New Project -> Single View App -> MySwiftPackageCreator

Add a swift file and name it as DateFormat.swift

Add the following code to DateFormat.swift

public class DateFormat {

    public static func formatDate(dateToFormat  date : Date) -> String {
        let dateFormat = DateFormatter()
        dateFormat.dateFormat = "YYYY-MM-dd"
        return dateFormat.string(from: date)
    }
}

The class and method is marked public because we will have this file bundled in swift package and the other modules later accessing our swift package will have access to this.

Also formatDate is simple method to format a date passed into "YYYY-MM-dd" format.

Now in change the viewDidLoad() in ViewController.swift to look like below

 override func viewDidLoad() {
        super.viewDidLoad()
        print("Formatted date is \(DateFormat.formatDate(dateToFormat: Date()))")
 }

Now build the Xcode project . You will see Formatted date is 2020-08-15 printed on Xcode console. Bingo our service is ready.

Step 2 : Create Swift Pacakage

Now Add new Swift Package to the same project. To do this follow the screenshots below.

Xcode -> New -> Swift Package

Screenshot 2020-08-15 at 9.01.48 AM.png

Name the package as TTDateFormatteer. Also select your MySwiftPackageCreator project for options Add to: and Group: as show below.

Screenshot 2020-08-15 at 9.05.18 AM.png

Finally your project structure should look like the below screenshot

Screenshot 2020-08-15 at 9.05.42 AM.png

Now open Package.swift you should see the following lines

.library(
            name: "TTDateFormatter",
            targets: ["TTDateFormatter"]
),

Screenshot 2020-08-15 at 9.02.03 AM.png

So the name in .library is place where all the package files are present. Do not change this name.

Now delete the file TTDateFormatter.swift in TTDateFormatter folder.

Drag the file DateFormat.swift into TTDateFormatter folder. Also copy to folder

Now build the project yow will see the error "Use of unresolved identifier 'DateFormat'" in ViewController.swift

This is because 'DateFormat' is now moved to Swift Package.

Add import TTDateFormatter, in ViewController.swift.

Now the error should go away. if the error persists change the target to TTDateFormatter and rebuild the Xcode project.

Screenshot 2020-08-15 at 4.46.40 PM.png

Now select TTDateFormatter package from the project navigator parallelly hold option key from the keyboard and drag the package to desktop. This ensures you drag the entire package.

Step 3 : Add Package to Github

Now create a repository named TTDateFormatter on your Github. Do not add any README.md file.

Now open terminal , cd into TTDateFormatter folder on desktop.

cd TTDateFormatter

Follow the commands below to push the Package to Github. (If comfortable push the package from Xcode itself).

Now in terminal once you are in TTDateFormatter folder

git init
git add .
git commit -m "Swift package TTDateFormatter"
git remote add origin https://github.com/TeaTalkInternal/TTDateFormatter.git
// Here add your repository URL
git push -u origin master
git tag 1.0.0
git push --tags

Now we have our TTDateFormatter package on Github.

Create a sample app to test our package

Go to get Xcode and create a single view application and name it as MyPackageTestFromRemote

Xcode -> New Project -> Single View App -> MyPackageTestFromRemote

Now go to project general settings screen and click on add framework button

Screenshot 2020-08-15 at 11.28.22 AM.png

In the libraries popup click on Add other, then select Add Package Dependency.

Screenshot 2020-08-15 at 11.28.09 AM.png

In the popup showing place to enter Package Repository Url add the url where SwiftPackage is hosted for me it is

github.com/TeaTalkInternal/TTDateFormatter...

Screenshot 2020-08-15 at 11.28.30 AM.png

Now the package is added .

Now open ViewController.swift change the code to look like below.

import UIKit
import TTDateFormatter


class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        print("Formatted date is \(DateFormat.formatDate(dateToFormat: Date()))")
    }
}

Run and see the result. You will see the Formatted date is 2020-08-15 printed the console.

Believe Me.... We successfully created swift package and used it too.

We stop here ...

Resources:

github.com/TeaTalkInternal/TTDateFormatter

My Reference :

youtube.com/watch?v=xu9oeCAS8aA