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: