简体   繁体   English

将本地 Swift Package 目标添加到现有 Xcode 项目

[英]Add a local Swift Package target to an existing Xcode project

I have an existing Xcode project that contains targets for a "traditional" iOS app (" Foo ") and an iOS framework (" FooLib ").我有一个现有的 Xcode 项目,其中包含“传统”iOS 应用程序(“ Foo ”)和 iOS 框架(“ FooLib ”)的目标。 When the Xcode 12 beta dropped a few days ago, I added a multiplatform (iOS/macOS) SwiftUI app target (" Foo SwiftUI ") to the same project.几天前,当 Xcode 12 beta 发布时,我在同一个项目中添加了一个多平台(iOS/macOS)SwiftUI 应用程序目标(“ Foo SwiftUI ”)。 Now I want to make my framework cross-platform, too, so I added a multiplatform Swift Package (" Foo Package ") to the project and moved the iOS framework classes to the new Swift package. Now I want to make my framework cross-platform, too, so I added a multiplatform Swift Package (" Foo Package ") to the project and moved the iOS framework classes to the new Swift package. I then added this package to all app targets' Frameworks, Libraries, and Embedded Content sections.然后我将此 package 添加到所有应用程序目标的框架、库和嵌入式内容部分。 It appeared to work for all app targets until I cleaned the project and tried again.在我清理项目并再次尝试之前,它似乎适用于所有应用程序目标。 Now the app targets complain that the Foo Package can't be resolved:现在应用程序目标抱怨Foo Package无法解决:

Missing package project 'FooLib'.

I tried removing & re-adding the FooLib package to the app targets, but it no longer shows up in the pop-up list of frameworks and libraries to add.我尝试将FooLib package 删除并重新添加到应用程序目标,但它不再显示在要添加的框架和库的弹出列表中。 Are local Swift packages explicitly unsupported in Xcode projects? Xcode 项目中是否明确不支持本地 Swift 包? Do I need to put the package in its own Git repo and set it up like any other third-party package dependency?我是否需要将 package 放入其自己的 Git 存储库中并像任何其他第三方 package 依赖项一样进行设置?

What I'm most confused about it that it looks like it worked right after I created the Swift package, but not after I cleaned the project and tried re-running the app targets.我最困惑的是,它看起来好像在我创建 Swift package 之后就可以正常工作,但在我清理项目并尝试重新运行应用程序目标之后却没有。

OK, sorry for my previous answer.好的,对不起我之前的回答。 — It is obvious how to share a package with yourself by way of a remote URL, such as GitHub, that serves it. — 很明显,如何通过远程 URL(例如 GitHub)与自己共享 package。 Basically you are playing the role of both developer and end-user, and it's clear how to do that.基本上,您扮演着开发人员和最终用户的角色,并且很清楚如何做到这一点。 So I think what's needed here is a step-by-step tutorial to sharing a package with yourself locally without actually serving it.所以我认为这里需要一个分步教程,在不实际提供服务的情况下与自己在本地共享 package。

  1. In Xcode, choose File > New > Swift Package and save as MyLibrary to the Desktop.在 Xcode 中,选择文件 > 新建 > Swift Package 并另存为 MyLibrary 到桌面。

  2. Now choose File > New > Project, choose the iOS App template, and save as App1 to the Desktop.现在选择 File > New > Project,选择 iOS App 模板,并保存为 App1 到桌面。

  3. You now have two windows open.您现在有两个 windows 打开。 Drag MyLibrary from the project navigator in the MyLibrary window into the empty area at the bottom of the project navigator in the App1 window.将 MyLibrary 从 MyLibrary window 中的项目导航器拖到 App1 window 中项目导航器底部的空白区域。

  4. Quit Xcode.退出 Xcode。

  5. Launch Xcode again and open App1.再次启动 Xcode 并打开 App1。 Look, the MyLibrary icon now has hierarchical contents.看,MyLibrary 图标现在具有分层内容。 Edit the app target and add MyLibrary library as a dependency.编辑应用程序目标并将 MyLibrary 库添加为依赖项。

  6. You can now edit MyLibrary;您现在可以编辑 MyLibrary; you can also import MyLibrary in your code and you will be able to use any public members of public types.您还可以在代码中导入 MyLibrary,您将能够使用公共类型的任何公共成员。

  7. Close App1 and create another project, App2.关闭 App1 并创建另一个项目 App2。 Repeat steps 3, 4, 5, and 6.重复步骤 3、4、5 和 6。

From now on, either App1 and App2 can "see" MyLibrary.从现在开始,App1 和 App2 都可以“看到”MyLibrary。 From either one, you can edit MyLibrary, you can import it and use its code.从任何一个中,您都可以编辑 MyLibrary,您可以导入它并使用它的代码。 But you cannot have both App1 and App2 open at the same time .但是您不能同时打开 App1 和 App2 If you do, one of them will complain that the package is missing.如果你这样做了,其中一个会抱怨 package 丢失。 So you'll be fine as long as you only have one at a time open.因此,只要您一次只打开一个就可以了。

Now, if you want to escape that limitation, then go back to the first way of dealing with all this: upload the package to GitHub and acquire it in your projects as a package dependency. Now, if you want to escape that limitation, then go back to the first way of dealing with all this: upload the package to GitHub and acquire it in your projects as a package dependency.

For me, it works using Xcode Workspaces “around” local Projects and Swift Packages.对我来说,它使用 Xcode 工作区“围绕”本地项目和 Swift 包工作。

  • Create App project创建应用项目
  • Create Swift Package创建 Swift Package
  • Create Workspace for app project为应用项目创建工作区
  • Add the App project to the Workspace将 App 项目添加到工作区
  • Add the Swift Package to the Workspace, too也将 Swift Package 添加到工作区
  • In the app target “General” settings, “embed&sign” the Swift Package from the current workspace在应用程序目标“常规”设置中,“嵌入并签名”当前工作区中的 Swift Package

You might have to restart Xcode afterwards for it to pick up on the changes...之后您可能需要重新启动 Xcode 才能了解更改...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM