简体   繁体   English

如果预览内容文件夹中有 SwiftUI 预览的代码,为什么存档会失败?

[英]Why does Archive fail if there is code for SwiftUI Previews in the Preview Content folder?

In my SwiftUI app, I have put some sample model objects in Xcode's "Preview Content" folder.在我的 SwiftUI 应用程序中,我将一些示例 model 对象放在 Xcode 的“预览内容”文件夹中。 I only access them in my SwiftUI PreviewProvider s.我只在我的 SwiftUI PreviewProvider中访问它们。 My understanding is that the code/assets in this folder are only used for previewing in the Canvas.我的理解是该文件夹中的代码/资产仅用于Canvas中的预览。 My previews work properly.我的预览工作正常。 I'm using Xcode 11.6 (11E708).我正在使用 Xcode 11.6 (11E708)。

However, when I go to Archive my app, it fails because it cannot find these sample model objects.但是,当我 go 归档我的应用程序时,它失败了,因为它找不到这些示例 model 对象。 I was under the impression that PreviewProvider code is not build into the final binary as of Xcode 11. See this question and this screenshot from the Xcode 11 release notes.我的印象是,自 Xcode 11 起, PreviewProvider代码并未构建到最终二进制文件中。请参阅此问题和 Xcode 11 发行说明中的此屏幕截图

(Curiously, I am able to compile with the Release configuration. It's just Archive that fails.) (奇怪的是,我能够使用 Release 配置进行编译。只是 Archive 失败了。)

As a workaround, I can put the #if DEBUG / #endif wrappers around my PreviewProvider but the above screenshot indicates that shouldn't be necessary.作为一种解决方法,我可以将#if DEBUG / #endif包装器放在我的PreviewProvider周围,但上面的屏幕截图表明这不是必需的。

Do I misunderstand how the "Preview Content" folder works?我是否误解了“预览内容”文件夹的工作原理?

I believe this is an Xcode bug, but I've thought of a workaround.我相信这是一个 Xcode 错误,但我想到了一种解决方法。

I put all my preview test data in its own PreviewProvider so that I can access it in other Previews.我将所有预览测试数据放在它自己的PreviewProvider中,以便我可以在其他预览中访问它。 The Archive still succeeds, and the test data will get stripped in the final Archive.存档仍然成功,并且测试数据将在最终存档中被剥离。 No #if DEBUG is needed.不需要#if DEBUG

Example (specific to my own use case):示例(特定于我自己的用例):

struct PreviewData: PreviewProvider {
    static var previews: some View {
        Text("This is test data for use in other previews.")
    }

    // Use enums for namespacing if desired
    enum Choices {
        static let deals = Choice(id: UUID().uuidString, text: "I want the best deals.", iconName: "bestDealsIcon")
        static let technology = Choice(id: UUID().uuidString, text: "I love technology!", iconName: "technologyIcon")
    }

Then I can access PreviewData.Choices.deals in other PreviewProvider s.然后我可以访问其他PreviewProvider中的PreviewData.Choices.deals

I had a very similar issue occur, only I was building an App Clip for an existing app.我遇到了一个非常相似的问题,只是我正在为现有应用程序构建App Clip I wasn't able to compile with the release scheme nor archive for distribution.我无法使用发布方案进行编译,也无法存档以进行分发。

The solution was to include the Preview Content directory under the development assets of the parent app's target, not the App Clip target itself.解决方案是将Preview Content目录包含在父应用程序目标的开发资产下,而不是App Clip目标本身。 That way, I got it to work in all cases such as SwiftUI previews, release builds, and archiving.这样,我让它在所有情况下都可以工作,例如 SwiftUI 预览、发布版本和存档。

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

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