All of a sudden Xcode says "Use of unknown directive #Preview" in SwiftUI

I work on my project on two computers. One of them is older and stuck at Xcode 13. My project targets iOS 15+.

Previously I was able to bounce back and forth between the two, checking code into source control in between.

After doing a bunch of work on one of them over an extended period, I've returned to my older one and checked out the code... only to be unable to build because Xcode balks at every use of #Preview. I didn't change any project settings in the meantime, so I have no idea why this is suddenly a problem...

Edit: It turns out that this is because previous versions of the SDK did not use this macro. Because I created a couple of new SwiftUI files on the newer OS and Xcode/SDK, it plopped #Preview into the new files instead of the previous implementation, PreviewProvider.

I'll leave this post up for anyone who searches on this problem, since I found zero other references in a general Web search.

Post not yet marked as solved Up vote post of Stokestack Down vote post of Stokestack
1.4k views

Replies

Hi,

Thanks for updating the post with your solution. You have correctly identified that older Xcode installations don't know about the #Preview and therefore would not be able to render a preview for it. For now, if you do need to open the source files in the older Xcode's you should stick to using a PreviewProvider.