Create a library using Swift Package Manager in Xcode

Often dev iOS we all know about CocoaPods and Carthage. Today I will introduce to you a new force announced by Apple at WDDC 2019 as Swift Package Manager. Let’s get into every one of the main content.

I. Swift Package Manger

Swift Packager Manager is a tool to help manage the distribution of source code, making it easy to share and re-use the code. Built in by Apple from Xcode 11 makes it simpler for us to manage dependencies. I’m just saying a little while you’re reading more https://swift.org/package-manager here.

II. Demo project and Swift Package Manager user guide
  1. First I will create a package to inherit UITextField to help format strings to VND (surely many dev will need this)

Open Xcode ➞ File ➞ New ➞ Swift Package

select the name of the package as above as “VNDTextField” here we have created Swift Package

After creating, we go to the package file and will proceed to the config in the following:

Here we pay attention:

  • products this is the definition we can see the files of the package when using other projects
  • platforms we add parameters to the operating system we want to build

After editing and setting up the file, we proceed to push the code to GitHub, and then import it again for the project you want to use. It’s simple, isn’t it!

Iii. Use Package

1.First we click on fiel .xcodeproj -> select Swift Packager, we will have as pictured, here I added my package to the demo project.

2. Then we click on the plus sign(+) in the lower left corner to add the package you want to the project, and if you want to delete the package you are using, click the minus sign(-) This is when we press the + sign

Here you paste into your package link created on github, waiting for a few seconds we will have the same result as me here:

I added the VNDTextField package to my class, and used it again as follows:

This is my demo class using VNDTextField again. We need to re-import the package in the class you want to use, then you can use what the package offers. After using VNDTextField, this is the result that I achieved.

It’s simple, with Swift Packager you can customize yourself common classes, reuse many times, help reduce the cost of rebuilding from scratch, more dynamic. At the same time, it is also easy for you to relax custom and expect many people to use it again easily. From now on, it has become much easier to use a third party or class common.

Through the article, hope you have a basic view of how to create a Swift Packager and use it again. Also add your iOS dev skill set on the fix bug luggage. Hope you love this article

I created a demo package here: https://github.com/ios-lib/VNDTextField

The copyright of the article belongs to codetoanbug.com. Thanks for reading!

Leave a Comment