你如何调试 Swift PlaygroundBook?

How Do You Debug Swift PlaygroundBook?

虽然我正在探索 Playground Book 示例代码,例如 this one,但我发现将代码获取到 运行 非常乏味,因为 Playground Book 在代码可以 运行:仅在 iPad 的 Swift 游乐场。

无法在 iOS 的 Swift Playground 中调试 "Sources" / 辅助代码,因为所有源文件都以不可编辑的纯文本显示。您必须打开 Xcode 中的源文件才能编辑它们,但是您无法编译或 运行 它们!

上面的示例代码特别繁琐,它使用了PlaygroundValue,一个需要Playground Book格式的持久化API,所以我仍然无法获得代码运行 通过将所有源代码分离到单独的 Playground 文件中 运行 Mac.

由于上面的示例代码已经过时,我发现几乎不可能正确调试它并将代码获取到 运行。你必须:

使用 Playground Book 高效调试和部署代码的工作流程是什么? 对我来说似乎不切实际我认为必须有更好的方法,但我对 Playground Book 不够熟悉并且我的在线研究没有产生任何合理的解决方法。

我不知道是否有任何可能性不需要您在实际 iPad 上测试 Playground。 无论如何,您可以通过

使开发 Swift Playgrounds 变得不那么乏味
  1. 使用 iCloud 将您的 mac 版本与 iPad 同步。
  2. 将您的 Playground 嵌入到 Xcode 项目中,如 中所述。这样,你至少可以在开发过程中实现自动完成。
  3. 将您的源文件链接到另一个目标,以便可以在 运行 Playground 之前捕获编译错误。

反正你还是会时不时遇到神秘的"Problem Running Playground"错误

根据我发送给 Apple 的错误报告/建议,我得到了以下回复:

We’ve actually built tools to help debug the auxilliary sources and we did a presentation at WWDC 2018 that demonstrates it. Please view the presentation and get access to the tools here: https://developer.apple.com/videos/play/wwdc2018/413/

经过进一步研究,我发现他们最近发布了一个 Playgrounds Author Template:

The Swift Playgrounds Author Template is a starter Xcode project that will help you create, debug, and produce a Playground book. Using the template you can step through the code for your live view as if it were an app so that you can identify bugs more easily and develop an efficient workflow for developing your Playground books.

此模板需要 Swift 4.1 到 运行,包括三个不同的目标:

  • PlaygroundBook
  • Book_Sources
  • LiveViewTestApp

您可以使用 LiveViewTestApp 在您的 Mac 上使用 Xcode 完全调试您的 Playground Book。