8

I have a simple Swift-based Hello World project -- a native Mac app that just has a simple "hello world" window -- built in Xcode using the "Cocoa Application" template. I'd like to add in a specific third-party Swift library.

I'm new to Xcode and Swift, so I'm missing something straightforward and haven't found a way to correctly add it so that I can import PG in the app and have it build:

Using the swift package manager

I can build the library using swift build. I can generate an Xcode project for the library using swift package generate-xcodeproj. But I haven't found clear instructions for bringing the library into an existing native Mac app in Xcode (or using swift build

For example, this tutorial from Twilio covers building a Swift project into a standalone binary, and editing Swift code in Xcode. But it doesn't cover including a Swift library in a native mac app. Other tutorials similarly focus on standalone swift tools.

I tried creating a Package.swift file in the app root, specifying the dependencies, and running swift package resolve and swift build. I get "error: could not find target(s): PG" from swift build:

import PackageDescription

let package = Package(
    name: "myapp",
    targets: [
        Target(
            name: "myapp",
            dependencies: ["PG"] // This must be incorrect 
        ),
    ],
    dependencies: [
    .Package(url: "https://github.com/davbeck/PG.swift.git", majorVersion: 0),
    ]
)

Other package mangers

I know how to add frameworks using CocoaPods, but this project isn't available for it. I tried with Carthage, but it didn't build a framework I could add to the project.

Simply adding the files

If simply add the Swift source by dragging and dropping the files, I get a "No such module PG" when I try to import PG. This also doesn't seem like a sustainable option. When dragging the files into Xcode, I had to select "Create groups" instead of the default "Create folder references" to get Xcode to see the files:

enter image description here

Matt Hampel
  • 5,088
  • 12
  • 52
  • 78

2 Answers2

5

One of the most straightforward ways to go here would be just adding the raw source files from the Sources directory in your Xcode project. This way you would not need to import anything but it also has a number of disadvantages. That's why package managers exist.

The project you linked actually supports Swift Package Manager installation. I suggest you try it instead of manually adding files. It's a little bit more complicated to use than Carthage or CocoaPods but it's actually worth it.

It's considered to be the best way to manage dependencies for macOS projects. On details of how to use Swift Package Manager, I suggest a couple of those useful tutorials.

Sergey Grischyov
  • 11,995
  • 20
  • 81
  • 120
  • Thanks Sergey! I've come across those tutorials but they don't really solve this question. Maybe the more direct question is: how _specifically_ do I add a library into an existing Xcode project using Swift Package Manager? I've clarified the question and what I've tried above. – Matt Hampel Jan 22 '18 at 20:24
1

I can feel your pain, because I went through some similar woes. I never used CocoaPods or Carthage for that. However, you really need to make sure you have tinkered with a few settings of Xcode before things start to work. I managed to solve all my problems doing what I have described here.

jvarela
  • 3,744
  • 1
  • 22
  • 43