Errors in SwiftUI Preview Code When Archiving for App Store Distribution in Xcode 16.0

Hi everyone,

I’m encountering a strange issue when trying to archive my iOS app for App Store distribution. The project builds and runs fine on “Any iOS Device (arm64)”, but when I try to Product → Archive, I get multiple errors related to preview sections in my SwiftUI view files. The app uses camera for photo and video capture.

Errors:

•	Cannot find 'PreviewCameraModel' in scope
•	Cannot infer contextual base in reference to member 'video'
•	Cannot infer contextual base in reference to member 'classify'

These errors only appear in code sections inside the #Preview blocks in SwiftUI files. Additionally:

  • When I click on an error in the Issue Navigator, the file shows the error momentarily but it disappears after less than a second.
  • The total error count decreases temporarily, but then it returns to the original number when clicking on other errors.
  • Build and Run works fine without any issues on devices and simulators, but these errors block the archiving process.

Workaround:

For now, I’ve resolved the issue by using #if DEBUG to exclude the preview code from release builds, but I’d prefer a cleaner solution if one exists.

System Details:

  • Xcode: 16.0
  • iOS Deployment Target: 16+
  • Swift: 5
  • Architecture: arm64

Has anyone encountered this issue or found a better way to handle SwiftUI preview code when archiving? Any advice on fixing this or insights into why the errors behave inconsistently during the archiving process would be appreciated.

Thanks in advance!

Hi,

Sorry to hear you are having problems getting your archive builds working due to the #Preview code.

If you need to be sure that the preview code is excluded from release builds then you'd need to wrap it in #if DEBUG (which is the workaround you already identified).

Errors in SwiftUI Preview Code When Archiving for App Store Distribution in Xcode 16.0
 
 
Q