Cannot preview SwiftUI

I am running Catalina (10.15) with XCode11. After creating a new project and selecting the option to Use SwiftUI, the file opens, but i get an error message in the preview window;


'Cannot preview in this file -- active scheme does not build this file. '


the info button says 'Select a scheme that builds a target which contains the current file, or add this file to a target that is built by the current scheme.'


i've tried opening numerous new projects, with and without unit tests, but none of them allow it to be previewed. I did see on Twitter that I'm not alone in seeing this issue. Any ideas on steps to troubleshoot it?


Thanks all.

Post not yet marked as solved Up vote post of xianritchie Down vote post of xianritchie
44k views
  • Changing preview mode from "My mac" to iPhone Dynamic Island worked for me

Add a Comment

Replies

Close all opened projects and open only one, cause it seems that "At most one Xcode project can be open that contains your shared SwiftUI views". https://developer.apple.com/forums/thread/673932

Using Xcode version 13.4.1 and faced the same problem when created new SwiftUI file. Xcode for some reading automatically turns off this toggle switch. Go to yourProject.xcodeproj -> Build Settings -> Build Options -> Enable Previews and toggle it back on to “Yes”.

PreviewProvider protocol is available from iOS 13 only. If you select the running target of your project to any simulator which is below iOS 13 the previews won't work. Just select anything iOS13 or above, pause and resume preview and it will work.

For me this was fixed by:

  1. Changing the build scheme from release to debug
  2. Clean build folder
  3. Build and launch on iPhone simulator.
  4. Stop running and close Xcode
  5. Reopen Xcode and click refresh on preview

This fixed it for me:

  • Quitting xCode
  • Starting xCode

When you start xCode/open a project, it takes a while in the upper toolbar to build the project, also the previews. As I let that run to the end (takes couple of seconds on Macbook Air M1), everything was fine. No changes needed.