Working with Workspaces in Swift

By Jesse Feiler

You can create a workspace in Swift that contains several projects. The workspace, project folders, and the files within them are managed by Xcode, subject to settings you create in the File inspector in the utilities area.

Within a project folder, you create files and groups of files using Xcode. The groups of files you see in the project navigator may or may not be folders on disk.

You can create a workspace with File→New→Workspace. A workspace is designed to contain multiple projects. This gives you two hierarchical layers you can use in developing apps:

  • A workspace can contain one or more projects.

  • A project can contain one or more targets.

One common use of a workspace is for two projects: one designed for iOS and the other for OS X. A shared folder (on disk) or group (in Xcode’s project navigator) may contain the shared code.

Multiple targets within a single project tend to have tighter integration and sharing than targets within a project. As you can see in the default templates, the targets within a project may consist of an app with automated tests as well as a user‐facing app. You can bring this structure forward so that within a single project, compiler options are used to build Pro and Lite versions.

A workspace is represented by a file with an extension of xcworkspace. When you are creating a new project, an option at the bottom of the sheet lets you place that project into a worksheet. (You also can add it to a project.)

image0.jpg

The project is placed in a target folder and is managed by an xcodeproj file. If you want to add an existing project to a workspace, drag the xcodeproj file into the navigator of the workspace. The file and folder structure of the project remains wherever you have placed it.

The figure shows how you add a new project to an existing project or workspace in Xcode: you can add a new project to an existing project or workspace using the pop‐up menu at the bottom of the sheet.

When you are adding a file with New→File, you can select the target to which to add it with checkboxes in the same location on the sheet as the pop-up menu shown. When you add a new target, you select the project to add it to with a pop‐up menu on the Options sheet where you specify the project name.

To sum up: Files go in targets; targets go in projects; and projects go in other projects or workspaces.

For convenience and simplicity, developers frequently place all of the workspace projects into a single workspace folder, but this is not necessary. In cases with complex workspaces, it’s sometimes not even possible.