简体   繁体   English

在 Xcode 11 中添加对本地 swift 包的依赖

[英]Add dependency on a local swift package in Xcode 11

I am trying to add a dependency on a local swift package in my Xcode project.我正在尝试在我的 Xcode 项目中添加对本地 swift 包的依赖。 (I don't want to add a dependency using a file url file://me/dev/app/package as this cannot be shared to other environments) (我不想使用文件 url file://me/dev/app/package添加依赖项,因为这不能共享到其他环境)

I tried to drag & drop the package to the target.我试图将包裹拖放到目标。 This creates a references to the package in the project that I was able to add in the "Link Binary With Libraries" build phase, but the library is still not found.这会在项目中创建对包的引用,我可以在“将二进制文件与库链接”构建阶段添加该项目,但仍未找到该库。

No such module 'Analytics'

My original configuration我的原始配置

在此处输入图像描述

After drag & drop拖放后

在此处输入图像描述

and here is my package configuration:这是我的包配置:

// swift-tools-version:5.1
import PackageDescription

let package = Package(
    name: "Analytics",
    products: [
        .library(name: "Analytics", targets: ["Analytics"]),
    ],
    dependencies: [
        .package(path: "../SomeOtherPackage"),
    ],
    targets: [
        .target(name: "Analytics", dependencies: [
            "SomeOtherPackage",
        ]),
    ]
)

I've also included the Package in the target linked binaries and/or bundle resources and/or compiled sources to no avail:我还在目标链接的二进制文件和/或捆绑资源和/或编译源中包含了包,但无济于事:

在此处输入图像描述

I've read this long discussion that doesn't seem to be conclusive.我读过这个似乎没有定论的长篇讨论

Here is a simple example of what I'm trying: https://github.com/gsabran/SPM_Xcode_test这是我正在尝试的一个简单示例: https ://github.com/gsabran/SPM_Xcode_test

Here is literally what I do and it just works... Xcode 11.2这就是我所做的,它只是工作...... Xcode 11.2

I. Prepare package一、准备包裹

  1. File > New > Swift Package > Select Empty Folder (or create new) > Enter Library Name (MyLibrary)文件 > 新建 > Swift 包 > 选择空文件夹(或新建)> 输入库名称 (MyLibrary)

  2. Here in project find MyLibrary.swift and make public structure & variable, so there is some export to use in app (I made static constant and fixed UT)在项目中找到 MyLibrary.swift 并制作公共结构和变量,因此有一些导出可以在应用程序中使用(我制作了静态常量并固定了 UT)

  3. Build > OK > Close Project构建 > 确定 > 关闭项目

II.二。 Prepare project准备项目

  1. File > New > Project > iOS > Single View App > Enter Name (MyApp) > Next > Select Same Folder as above (it is by default) > Create File > New > Project > iOS > Single View App > Enter Name (MyApp) > Next > 选择与上面相同的文件夹(默认情况下)> Create

  2. Build > OK构建 > 确定

  3. From Finder drag MyLibrary folder right below project name in Xcode Project Navigator从 Finder 中将 MyLibrary 文件夹拖到 Xcode Project Navigator 中项目名称的正下方

  4. Build > OK构建 > 确定

  5. Click MyApp project icon in Project Navigator > Select MyApp application target > section Frameworks, Libraries, … click + > In topmost Workspace section select MyLibrary > Add单击 Project Navigator 中的 MyApp 项目图标 > 选择 MyApp 应用程序目标 > Frameworks, Libraries, … 单击 + > 在最顶部的 Workspace 部分选择 MyLibrary > Add

  6. Clean > Build > OK清洁 > 构建 > 确定

  7. Open ContentView.swift > type import MyLibrary (autocompletion already see it) Replace “Hello, World” with "(MyLibrary.text)"打开 ContentView.swift > 输入import MyLibrary (自动补全已经看到了)将“Hello, World”替换为“(MyLibrary.text)”

  8. Build & Run > OK构建并运行 > 确定

That's it.而已。

To verify used relative path open project.pbxproj in TextEdit, here is screenshot要验证使用的相对路径,请在 TextEdit 中打开 project.pbxproj,这里是屏幕截图

Swift 包相对路径

Update:更新:
Note1 - I've considered some delay in package resolving after Clean, so during some period of time, 1-2 mins here, Build fails exactly with reported error, but after that delay it is visible that index restarted and following Build succeeded.注意 1 - 我考虑过在 Clean 之后包解析有一些延迟,所以在一段时间内,在这里 1-2 分钟,Build 完全失败并报告错误,但在该延迟之后可以看到索引重新启动并且 Build 成功。
Note2 - Adding second dependent package in graph (MyApp > MyLibrary > AnotherLibrary) is the same.注 2 - 在图中添加第二个依赖包(MyApp > MyLibrary > AnotherLibrary)是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将本地package添加到Xcode Swift项目中? - How to add local package to Xcode Swift project? 如何使用 Swift package 管理器添加依赖项 - How to add dependency using Swift package manager Xcode 11 Swift软件包管理器无法解析软件包 - Xcode 11 swift package manager unable resolve packages 使用 Swift Package 管理器为二进制目标添加 package 依赖项 - Add package dependency for a binary target with Swift Package Manager Swift Package 管理器 - 将 package 依赖项添加到您的 Package.swift 以进行本机反应 - Swift Package Manager - add the package dependency to your Package.swift for react native 如何将核心数据添加到现有的 Xcode 9 Swift 4 iOS 11 项目? - How to add Core Data to existing Xcode 9 Swift 4 iOS 11 project? 如何在 Swift 包管理器中添加二进制框架作为依赖项? - How to add Binary framework in Swift Package Manager as dependency? 将 material-components-ios 作为 package 依赖项添加到 Xcode 11 项目失败 - Adding material-components-ios as a package dependency to an Xcode 11 project fails 如何在 xcode 13 中添加 Swift package 管理器作为动态库? - How to add a Swift package manager as a dynamic library in xcode 13? Xcode 9:Swift依赖性分析错误 - Xcode 9: Swift dependency analysis error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM