What build setting causes `PreviewProvider`s to be stripped when archiving?

I'm having trouble creating an archive of my development build for internal testing. When archiving, it encounters errors such as:

error: cannot find 'MockDataModel' in scope

This type is used within a PreviewProvider, and the file containing this type is included in the "Preview Content" folder, which is listed in the "Development Assets" of the project settings.

As expected, the "Preview Content" directory is properly excluded from the archive, but what is not expected is that the PreviewProvider where it is referenced is included while compiling. This post indicates that it is dead code and will be stripped out when archiving.

I'm hoping to get a better understanding of which build setting(s) or other mechanism determine when PreviewProviders are excluded from a build.

Cheers!

Replies

Hi,

Sorry to hear you are having problems getting the previews builds working as you expect. To make sure the PreviewProvider is not included in your archive build you'll have to wrap them in #if DEBUG <…> #endif.

We understand that this is confusing, and we are constantly looking for ways to improve the experience of using Previews without it affecting your final build and shipping products.

  • Please read my suggestion below for Apple to come up with a property wrapper to mark SwiftUI preview structs. Hopefully it gets fixed. Currently there is a lot of code with #if DEBUG <…> #endif

Add a Comment

Your solution to use #if DEBUG <…> #endif however it is not very obvious before reading your post.

I am wondering why SwiftPreview structs would ever be included in the archive?

I wish Apple could come up with a property wrapper (example: @SwiftUIPreview or @Devlopment) that could be used for previews, that way there might be a way to avoid the inclusion of code marked with this property wrapper in the archive solving this problem.

My team ran into the same problem. We fixed the issue by removing the Swift file from Developer Assets. Our understanding is that the preview mocks will be stripped since they are only used in the PreviewProvider types that are also stripped. I haven't run the validation suggested in the post you linked, though.

This article was helpful to my understanding:

To be clear; we’re not adding these Swift files as Development Assets as we did before with our preview assets catalog. The reason is that the compiler would fail during archiving as the code we referenced is no longer present. You could solve this using #if DEBUG checks, but that’s not always nice to use. Instead, relying on dead code stripping makes much more sense.

https://www.avanderlee.com/xcode/development-assets-preview-catalog/

  • You might want to verify that part about dead code stripping for yourself.

    We did some tests and turned Dead Code Stripping on and off (Build Settings) and our xcarchive file was exactly the same size (did not strip out the code used mock preview data).

Add a Comment