Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Swift 3 packages to Xcode 8 using the Swift package manager

I am new to Swift and Xcode. I am running macOS Sierra and Swift 3.

For my first forays in Swift, I am developing a simple command line tool. There are a couple of Swift packages that I want to use and the installation instructions for both packages on GitHub says to use the Swift package manager by simply adding them as dependencies in the package manifest file.

What I can't figure out is how to do this in Xcode. Do I just create a 'package.swift' file in the root of my project? Doing this and then running the project doesn't seem to work as the required packages don't seem to be added to my project.

Am I doing something wrong?

like image 262
Garry Pettet Avatar asked Sep 26 '16 17:09

Garry Pettet


People also ask

How do I install Swift package manager?

To create a new Swift package, open Xcode and select File > New > Swift Package. Choose a name and select a file location. Select “Create Git repository on my Mac” to put your package under version control. On completion, the Swift package opens in Xcode and looks similar to a standard Xcode project.

How do I add Swift packages to Xcode?

To add a package dependency to your Xcode project, select File > Swift Packages > Add Package Dependency and enter its repository URL.

How do I update Swift package dependencies?

To resolve, Open the project from the project panel, select the project (not the targets), then select the "Swift Packages" tab. Double click on the package you want to update and change the minimum version to the next major version.


1 Answers

Xcode and the SPM can work together, but as far as I can tell you do need to take one step on the command line.

Put your package manifest file into the same directory as the Xcode project, and then invoke swift package generate-xcodeproj

The package manager will pull down your dependencies and rewrite the .xcodeproj file to refer to them.

It will preserve any existing source, but the directory structure will be reconfigured to SPM's preferred arrangement:

PROJECT_DIR ├── Sources │   └── ProjectName │       ├── YourCode.swift │       └── YourOtherCode.swift ├── Dependencies │   └── SomeDependency │       ├── DependencyCode.swift │       └── OtherDependencyCode.swift └── Package.swift 

N.B., I haven't tested this extensively on a live project; given the fact that SPM docs still say WIP, please make sure you've made a recent commit.

like image 200
jscs Avatar answered Oct 28 '22 23:10

jscs