在Xcode中为Mac原生Swift项目添加PG.swift库遇阻求助
Hey there, let's break down the most reliable ways to get PG.swift integrated into your Mac Cocoa app—since you've already hit a few roadblocks with different tools, let's focus on the native Swift Package Manager (SPM) approach first, as it's the best fit for Swift libraries like PG.swift.
Option 1: Add SPM Dependency Directly in Xcode (Easiest Method)
This is the simplest way, and you don't need to mess with manual Package.swift edits or generating Xcode projects separately:
- Open your existing Cocoa Application project in Xcode.
- Click on your project's root file (the blue icon at the top of the left sidebar) to open the project settings.
- Switch to the Package Dependencies tab.
- Click the
+button in the bottom-left corner. - In the pop-up window, paste the Git repository URL for PG.swift (you'll find this on the library's GitHub/GitLab page) and click Add Package.
- Select the version rule you want (e.g., "Up to Next Major Version" for stability) and click Add Package again.
- On the final screen, make sure the
PGtarget is checked, then select your Cocoa Application from the "Add to Target" dropdown. Click Add Package.
Once this is done, Xcode will automatically resolve the dependency, build PG.swift, and link it to your app. You can now import PG in your Swift files without any errors.
Why Your Manual Package.swift Failed
If you wanted to use a manual Package.swift for your project, the error could not find target(s): PG usually means your dependency or target configuration was off. Here's a corrected example you can use:
// swift-tools-version: 5.9 import PackageDescription let package = Package( name: "YourHelloWorldApp", platforms: [.macOS(.v13)], // Match your app's deployment target products: [ .application(name: "YourHelloWorldApp", targets: ["YourHelloWorldApp"]), ], dependencies: [ // Replace with PG.swift's actual repo URL and minimum version .package(url: "https://github.com/[username]/PG.swift.git", from: "1.0.0"), ], targets: [ .target( name: "YourHelloWorldApp", dependencies: [ // Make sure the product name matches what PG.swift defines in its own Package.swift .product(name: "PG", package: "PG.swift") ] ), ] )
After creating this file in your project root:
- Run
swift package resolvein Terminal to fetch dependencies. - Run
swift package generate-xcodeprojto create an Xcode project that includes PG.swift. - Open the generated
.xcodeprojfile, and you should be able toimport PGsuccessfully.
Fixing the Drag-and-Drop Method (If You Prefer It)
If you tried dragging PG.swift's source files and got No such module PG, that's because you didn't set it up as a separate module. Here's how to fix it:
- Drag the entire
Sources/PGdirectory (from PG.swift's repo) into your Xcode project. When prompted, choose Create folder references (not groups) and make sure "Copy items if needed" is checked. - In Xcode, go to File > New > Target, select Framework under macOS, name it
PG, and set the language to Swift. - Delete the default
PG.swiftfile created for the framework. - Drag all the files from the
PGfolder reference you added earlier into the newPGframework target's Compile Sources build phase. - Go to your Cocoa Application target's General tab, scroll to Frameworks, Libraries, and Embedded Content, click the
+button, and add thePGframework.
Now you can import PG in your app's code, and this setup is sustainable (you can update the source files later without reconfiguring everything).
Why CocoaPods/Carthage Didn't Work
As you noticed, CocoaPods doesn't support PG.swift (likely because the library doesn't have a Podspec), and Carthage might fail if PG.swift's build configuration isn't set up for framework generation. SPM is the officially supported tool for Swift libraries, so it's the best long-term solution here.
内容的提问来源于stack exchange,提问作者Matt Hampel

