简体   繁体   中英

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. I only access them in my SwiftUI PreviewProvider s. My understanding is that the code/assets in this folder are only used for previewing in the Canvas. My previews work properly. I'm using Xcode 11.6 (11E708).

However, when I go to Archive my app, it fails because it cannot find these sample model objects. 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.

(Curiously, I am able to compile with the Release configuration. It's just Archive that fails.)

As a workaround, I can put the #if DEBUG / #endif wrappers around my PreviewProvider but the above screenshot indicates that shouldn't be necessary.

Do I misunderstand how the "Preview Content" folder works?

I believe this is an Xcode bug, but I've thought of a workaround.

I put all my preview test data in its own PreviewProvider so that I can access it in other Previews. The Archive still succeeds, and the test data will get stripped in the final Archive. No #if DEBUG is needed.

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.

I had a very similar issue occur, only I was building an App Clip for an existing app. 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. That way, I got it to work in all cases such as SwiftUI previews, release builds, and archiving.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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