You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Xcode中为Mac原生Swift项目添加PG.swift库遇阻求助

How to Add PG.swift to Your Cocoa Application in Xcode

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 PG target 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:

  1. Run swift package resolve in Terminal to fetch dependencies.
  2. Run swift package generate-xcodeproj to create an Xcode project that includes PG.swift.
  3. Open the generated .xcodeproj file, and you should be able to import PG successfully.

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/PG directory (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.swift file created for the framework.
  • Drag all the files from the PG folder reference you added earlier into the new PG framework 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 the PG framework.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:45:55