[英]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一、准备包裹
File > New > Swift Package > Select Empty Folder (or create new) > Enter Library Name (MyLibrary)文件 > 新建 > Swift 包 > 选择空文件夹(或新建)> 输入库名称 (MyLibrary)
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)
Build > OK > Close Project构建 > 确定 > 关闭项目
II.二。 Prepare project
准备项目
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
Build > OK构建 > 确定
From Finder drag MyLibrary folder right below project name in Xcode Project Navigator从 Finder 中将 MyLibrary 文件夹拖到 Xcode Project Navigator 中项目名称的正下方
Build > OK构建 > 确定
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
Clean > Build > OK清洁 > 构建 > 确定
Open ContentView.swift > type import MyLibrary
(autocompletion already see it) Replace “Hello, World” with "(MyLibrary.text)"打开 ContentView.swift > 输入
import MyLibrary
(自动补全已经看到了)将“Hello, World”替换为“(MyLibrary.text)”
Build & Run > OK构建并运行 > 确定
That's it.而已。
To verify used relative path open project.pbxproj in TextEdit, here is screenshot要验证使用的相对路径,请在 TextEdit 中打开 project.pbxproj,这里是屏幕截图
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.