在Xcode 11中添加对本地swift包的依赖 [英] Add dependency on a local swift package in Xcode 11

查看:688
本文介绍了在Xcode 11中添加对本地swift包的依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的Xcode项目中添加对本地swift包的依赖. (我不想使用文件URL file://me/dev/app/package添加依赖项,因为它不能与其他环境共享)

I am trying to add a dependency on a local swift package in my Xcode project. (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)

我试图拖动&将包放到目标位置.这将创建对项目中程序包的引用,我可以在使用库链接二进制文件"构建阶段中添加该程序包,但仍找不到该库.

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'

我的原始配置

拖放后放下

这是我的软件包配置:

// 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.

这是我要尝试的一个简单示例: https://github.com/gsabran/SPM_Xcode_test

Here is a simple example of what I'm trying: https://github.com/gsabran/SPM_Xcode_test

推荐答案

从字面上看,这就是我的工作,它确实可以正常工作... Xcode 11.2

Here is literally what I do and it just works... Xcode 11.2

I.准备包裹

  1. 文件>新建> Swift软件包>选择空文件夹(或创建新文件夹)>输入库名称(MyLibrary)

  1. File > New > Swift Package > Select Empty Folder (or create new) > Enter Library Name (MyLibrary)

在项目中找到MyLibrary.swift并进行公共结构&变量,因此在应用程序中有一些导出要使用(我将静态常量和固定的UT设为了)

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)

构建>确定>关闭项目

II.准备项目

  1. 文件>新建>项目> iOS> Single View App>输入名称(MyApp)>下一步>选择与上述相同的文件夹(默认情况下)>创建

  1. File > New > Project > iOS > Single View App > Enter Name (MyApp) > Next > Select Same Folder as above (it is by default) > Create

构建>确定

从Finder中,将MyLibrary文件夹拖到Xcode Project Navigator中项目名称的正下方

From Finder drag MyLibrary folder right below project name in Xcode Project Navigator

构建>确定

在项目导航器"中单击MyApp项目图标>选择MyApp应用程序目标>框架,库"部分,…单击+>在最上方的工作区"部分中,选择"MyLibrary">添加"

Click MyApp project icon in Project Navigator > Select MyApp application target > section Frameworks, Libraries, … click + > In topmost Workspace section select MyLibrary > Add

清洁>构建>确定

打开ContentView.swift>键入import MyLibrary(自动补全已经看到了) 用((MyLibrary.text)""代替"Hello,World"

Open ContentView.swift > type import MyLibrary (autocompletion already see it) Replace "Hello, World" with "(MyLibrary.text)"

构建并构建运行>确定

就这样.

要验证使用的相对路径,请在TextEdit中打开project.pbxproj,这是屏幕截图

To verify used relative path open project.pbxproj in TextEdit, here is screenshot

更新:
Note1 -我考虑过在清理后解决程序包延迟问题,因此在一段时间内(此处为1-2分钟),Build完全失败,并报告了错误,但是在该延迟之后,该索引可见重新启动,并且构建成功.
Note2 -在图中添加第二个从属程序包(MyApp> MyLibrary> AnotherLibrary)是相同的.

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.
Note2 - Adding second dependent package in graph (MyApp > MyLibrary > AnotherLibrary) is the same.

这篇关于在Xcode 11中添加对本地swift包的依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆