Swift Packages are reusable code components. In addition to adding other Swift package dependencies to Swift packages, we can also add Swift package dependencies to Xcode projects. This article will introduce how to add remote and local Swift package dependencies in Xcode projects.
The complete code for this chapter can be found in .
Table of Contents
Adding Remote Swift Package Dependencies
In Xcode projects, adding a remote Swift package is simple. We’ll cover how to add the Toast-Swift package.
Click the GreetingApp project under PROJECT, click Package Dependencies, and then click the + button.
Xcode will pop up the following dialog. In the search field in the upper right corner, fill in Toast-Swift’s Git repository URL: https://github.com/scalessec/Toast-Swift. Xcode will automatically fetch the required information from this URL.
The Dependency Rule field has the following options:
- Up to Next Major Version: Allows the project to update to the next major version.
- Up to Next Minor Version: Allows the project be update to the next minor version.
- Range of Versions: Specify a range of versions that are allowed to update.
- Exact Version: Specify a specific version.
- Branch: Use branch-based dependencies.
- Commit: Specify a specific commit hash.
Select the product vended by the Toast-Swift package. A Swift package may vend several products. We just need to select the products we need.
After the addition is complete, you can see that the Toast-Swift package has been added to the Package Dependencies list.
Click the GreetingApp target under TARGETS, we can see that Toast-Swift has also been added to the Frameworks, Libraries, and Embedded Content list.
This completes the adding Swift package dependencies. The following code shows how to use the Toast-Swift package in your Xcode projects.
import UIKit import Toast class ViewController: UIViewController { @IBOutlet weak var greetingTextField: UITextField! @IBOutlet weak var greetingLabel: UILabel! @IBAction func onClick(_ sender: Any) { view.makeToast("Hello Wayne's Talk!") } override func viewDidLoad() { super.viewDidLoad() } }
Adding Local Swift Package Dependencies
We can use Swift packages to modularize our source code. We first create a Swift package and move some code into it. Then, in the original project, add the newly created Swift package dependency. This is adding dependencies on local Swift packages. If you don’t know how to build a Swift package, you can refer to the following articles.
Click the GreetingApp project under PROJECT, click Package Dependencies, and then click the + button. Xcode will pop up the following dialog. At the bottom of the dialog, click the Add Local… button.
Xcode will pop up the following dialog, let us select the fold of the local Swift package. We choose the Greeting package, you can find the source code of Greeting in the code example.
After selecting the package, you can see that the Greeting package is added to the Packages list in the Navigator window on the left. So far, you can’t use the Greeting package in the project. You must also add the Greeting package to the Frameworks, Libraries, and Embedded Content list.
Click the + button under Frameworks, Libraries, and Embedded Content.
Xcode will pop up the following dialog. We choose the Greeting package library.
After that, the Greeting package library will be added to the Frameworks, Libraries, and Embedded Content list.
At this time, you can use the Greeting package in the project. The following code shows how to use the Greeting package in your projects.
import UIKit import Toast import Greeting class ViewController: UIViewController { @IBOutlet weak var greetingTextField: UITextField! @IBOutlet weak var greetingLabel: UILabel! @IBAction func onClick(_ sender: Any) { let text = greetingTextField.text ?? "" greeting.setTextFromJSON("{\"text\":\"\(text)\"}") greetingLabel.text = greeting.text view.makeToast(greeting.text) } private var greeting = Greeting() override func viewDidLoad() { super.viewDidLoad() greetingLabel.text = greeting.text } }
Conclusion
Swift Packages are reusable code components. In addition to adding open-source Swift packages to Xcode projects, we can also modularize our source code. Try using local Swift packages in your project to modularize the source code in your project!