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:
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.
Add a New Target to Your Project You can also add new apps, system extensions, test suites, and other types of targets to your project. To add a new target: Choose File > New > Target. Select the platform for the new target.
Swift is free and open source, and it's available to a wide audience of developers, educators, and students under the Apache 2.0 open source license. We're providing binaries for macOS and Linux that can compile code for iOS, macOS, watchOS, tvOS, and Linux.
To run your app in Simulator, choose an iOS simulator—for example, iPhone 6 Plus, iPad Air, or iPhone 6 + Apple Watch - 38mm—from the Xcode scheme pop-up menu and click Run. Xcode builds your project and then launches the most recent version of your app running in Simulator on your Mac screen, as shown in Figure 1-1.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With