24

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?

4
  • Which packages are you trying to install? Commented Sep 26, 2016 at 17:49
  • Specifically github.com/onevcat/Rainbow Commented Sep 26, 2016 at 18:01
  • Install it via cocoapods! See cocoapods website for installation guide Commented Sep 26, 2016 at 18:04
  • 3
    @Do2 The question is clearly about Swift packages. Answering someone (in comments, no less) by telling them to use something else is unhelpful. Commented Mar 31, 2018 at 15:35

2 Answers 2

42

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. Works a charm. +1 for the safety advice too!
Is there any way to force SPM not to change the directory structure inside the .xcodeproj file? I'm not too fussed about all my files being inside the Sources folder but I also have groups inside of the navigator that are pointing to the Sources folder and I'd like them to remain intact.
A note to anyone who performs these steps then gets a "no module foo found" compiler error. As of version 9.2 Xcode provided me two recommended project changes, one of which was setting Swift 3 @objc Inference to default. After performing these changes my target recognised the external dependency module for import.
Thanks for the update, @Airuop. Haven't don this in a while; I've made myself a todo to try it and make sure the answer isn't missing anything else.
0

In addition to running swift package generate-xcodeproj, I had to build my project in Xcode before I could use the installed packages.

1 Comment

Makes total sense, as the package manager downloads the packages, then to use them you have to compile them.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.