Xcode is unable to Archive a project with Preview Content

Originator:jeremy.gale
Number:rdar://FB8969539 Date Originated:2021-01-13
Status:Open Resolved:
Product:Xcode Product Version:Xcode Version 12.2 (12B45b)
Classification:Incorrect/Unexpected Behavior Reproducible:Always
 
Please describe the issue:

When creating a simple SwiftUI app project with code in the “Preview Content” folder, I am not able to Archive my project. I get a compilation error that the objects cannot be found.

Here is an excerpt from WWDC 2020 talk “Structure Your App for SwiftUI Previews” (Link with timecode: https://developer.apple.com/videos/play/wwdc2020/10149/?time=644)

“If you're creating a SwiftUI app form an Xcode template, the app comes pre-configured with a development asset path for you….<snip> What's great about Development Assets is they apply not only to files like Asset catalogs, but also to code. Let's look at what's inside that preview content folder that we just added.
<snip>
By using the navigator, we can look inside and see two Swift files. These Swift files contain code that I only need for development and debugging and testing my app, and I don't want to include these when my app is actually deployed.”

From this video, I expect I should be able to use the content of this folder while in development mode, but it will be ignored/stripped while Archiving my project and deploying it to the App Store. However, it doesn’t seem to work that way.


Please list the steps you took to reproduce the issue:
* I’ve created a simple SwiftUI project using the standard app template. I’ve attached a .zip file of it here.
* I have not changed any build settings
* I’ve put some code in the Preview Content. It contains some simple sample model objects that I want to use in my Xcode Previews
* I use those model objects in my Xcode Previews while developing. Everything works perfectly. I am even able to run it on a device without any problem.
* I tried to Archive the project. I get a build error “Cannot find 'timCook' in scope”


What did you expect to happen?

I expect to be able to Archive this project without any problem. I expect that the sample code in Preview Content is not needed. 

What actually happened?

It gives a compilation error that one of the sample model objects cannot be found. This model object is *only* referenced in a PreviewProvider. 

As a workaround, I can wrap my PreviewProvider struct with #if DEBUG / #endif, but I was under the impression that shouldn’t be necessary. The Xcode 11 release notes say there was a Resolved Issue for “The #if / #endif compiler conditionals surrounding PreviewProvider types have been removed from SwiftUI templates. PreviewProviders aren’t properly removed from built products when archived.”

Comments

@Thomas.DDelgado - I thought of a workaround that doesn't use #if DEBUG and answered the question on StackOverflow: https://stackoverflow.com/questions/63078880/why-does-archive-fail-if-there-is-code-for-swiftui-previews-in-the-preview-conte

By jeremy.gale at April 8, 2021, 3:11 p.m. (reply...)

I'm also having the same problem. Is there any way to solve this without using this workaround?

By Thomas.DDelgado at Feb. 10, 2021, 5:01 p.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!