After having modularized a project, the project is divided into an app project or a framework project, and several Swift packages. We can easily manage these sub-projects with Xcode workspaces. This article will introduce how to use Xcode Workspaces to manage several sub-projects.
The complete code for this chapter can be found in .
Table of Contents
Creating a Xcode Workspace
Click File -> New -> Workspace to create a workspace.
Enter Greeting as the name of the workspace. Create a folder called iOSWorkspaceExample, and create the Greeting workspace under this folder.
The Greeting workspace is created. But currently it doesn’t contain any projects.
Creating a Swift Package in Workspace
Now we are going to create a Swift package in the Greeting workspace. Click File -> New -> Package.
Enter GreetingPhrases as the name of the Swift package and choose to create it under the iOSWorkspaceExample folder. In the Add to and Group fields at the bottom of the dialog, we choose to add GreetingPhrases to the Greeting workspace and to the Greeting group.
The Greeting workspace now includes a Swift package.
Creating a Framework Project in Workspace
Next we will create a framework in the Greeting workspace. Click File -> New -> Project.
Choose to create a Framework project.
Enter GreetingUI as the name of the framework.
Choose to create GreetingUI under the folder iOSWorkspaceExample. In the Add to and Group fields at the bottom of the dialog, we choose to add GreetingUI to the Greeting workspace and to the Greeting group.
The Greeting workspace now contains a Swift package and a framework.
Creating an App Project in Workspace
Finally, we will create an App in the Greeting workspace. Click File -> New -> Project.
Choose Create an App Project.
Enter GreetingApp as the name of the App.
Choose to create GreetingApp under the folder iOSWorkspaceExample. In the Add to and Group fields at the bottom of the dialog, we choose to add GreetingApp to the Greeting workspace and to the Greeting group.
Now the Greeting workspace contains an app, a framework, and a Swift package.
Add Swift Package Dependency to Framework
Both GreetingUI and GreetingPhrases are inside the Greeting workspace. So GreetingUI does not need to add GreetingPhrases dependency. Xcode workspace will automatically help us handle the dependencies between all projects in the workspace. But GreetingUI doesn’t yet work with GreetingPhrases. It must add GreetingPhrases to its Frameworks and Libraries list. And this step must be done by us, Xcode will not handle it for us.
Select the GreetingUI target under TARGETS of GreetingUI, and click the + button under Frameworks and Libraries.
Choose to add the GreetingPhrases library.
After adding the GreetingPhrases library to the Frameworks and Libraries list of GreetingUI, GreetingUI can use GreetingPhrases.
The following code shows how to use GreetingPhrase in GreetingUI.
import UIKit import GreetingPhrases public class GreetingViewController: UIViewController { public static func newInstance() -> GreetingViewController { let storyboard = UIStoryboard(name: "Greeting", bundle: Bundle(for: Self.self)) let viewController = storyboard.instantiateViewController(withIdentifier: "GreetingViewController") as! GreetingViewController return viewController } @IBOutlet weak var greetingLabel: UILabel! public override func viewDidLoad() { super.viewDidLoad() greetingLabel.text = GreetingPhrases().text } }
Add Swift Package and Framework dependencies to App
GreetingApp does not need to add the GreetingPhrases dependency, because the Xcode workspace will automatically handle it for us. But GreetingApp must add GreetingPhrases and GreetingUI to its Frameworks and Libraries list.
Select the GreetingApp target under TARGETS of GreetingApp, and click the + button under Frameworks and Libraries.
Choose to add the GreetingPhrases library.
Click the + button again and choose to add the GreetingUI framework.
After adding the GreetingPhrases library and the GreetingUI framework to the GreetingApp’s Frameworks and Libraries list, the GreetingApp can use them.
The following code shows how to use GreetingPhrase and GreetingUI in GreetingApp.
import UIKit import GreetingUI import GreetingPhrases class ViewController: UIViewController { @IBOutlet weak var showButton: UIButton! @IBAction func onClick(_ sender: Any) { let viewController = GreetingViewController.newInstance() present(viewController, animated: true) } override func viewDidLoad() { super.viewDidLoad() showButton.setTitle("Show \(GreetingPhrases().text)", for: .normal) } }
Executing App
Because Greeting workspace has three schemes, remember to choose GreetingApp to execute. Even if we modify the code in GreetingUI or GreetingPhrases, we can directly execute GreetingApp, instead of switching to GreetingUI or GreetingPhrases to compile first, and then switch back to GreetingApp to execute. When we execute GreetingApp, Xcode will automatically recompile GreetingUI and GreetingPhrases for us. Isn’t it very convenient!
Conclusion
Swift packages allow us to modularize a project by splitting it into several Swift packages. Or when we are developing a framework, we need an app to test it. In other words, when our project has several sub-projects, Xcode workspace can help me effectively manage the dependencies between them.