'SwiftUI - Unknown preview provider "ContentView_Previews_" when previewing. Happens in a brand-new project
I have this simple view.
import SwiftUI
struct ContentView: View {
var body: some View {
Text("Hello")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Usually this previews fine. But today, I get this error Unknown preview provider "ContentView_Previews_":
Pressing Try Again doesn't work. When I press Diagnostics, this shows up:
RemoteHumanReadableError: Failed to update preview.
Error encountered when sending 'previewInstances' message to agent.
==================================
| RemoteHumanReadableError: Unknown preview provider "ContentView_Previews_"
|
| 5SwiftUI does not contain a preview provider named "ContentView_Previews_". Check your build settings to ensure the preview provider is compiled into your product.
|
| Mangled name: 009_SwiftUI_0021ContentView_Previews_V
So I thought maybe Xcode is glitching out and wants an underscore at the end of the preview struct. I added that:
struct ContentView_Previews_: PreviewProvider {
But now I get, Unknown preview provider "ContentView_Previews__
.
Is anyone else coming across this? My Xcode version is Version 12.3 (12C33).
Solution 1:[1]
I had the very same problem. Eventually, I figured out that this happens when the project name starts with a number character, e.g. "01-test"
. Creating a new project without the digit as a first character works just fine, e.g. "test"
.
Solution 2:[2]
None of above answer worked for me so after a lot of trying I found out that my preview struct that conforms to PreviewProvider
is private. That solved my issue.
Solution 3:[3]
I have the same problem in my project. Project I'm working on has name starting with number character and Xcode replace that character with underscore _
in few places in project files. Exactly the same underscore is added at the end of Service Provider and because of that all SwiftUI previews stoped working with error Unknown preview provider "ContentView_Previews_
The only solution that works is to remove numeric characters at the start of project name as in answer provided by Sorceror.
Unfortunately not always we can change project name to fix the issue and it ends up with working on SwiftUI project without previews - old way has to be used - build and run entire project to see changes in simulator or real device.
Unfortunately I didn't found another way to fix that issue and get SwiftUI previews work as they should.
Solution 4:[4]
My project name had no numbers. The error showed up after I moved the PreviewProvider struct to the bottom of the file. Once I moved it back to just below the main view, the error cleared and the preview worked again. ? Just a bit of Xcode madness, lmao.
Solution 5:[5]
In my case I did not have any numbers of specific characters in the name of my project.
Clean build also did not work.
Solution for me was changing
#if Debug
your code
#endif
to
#if DEBUG //make with capital letters
your code
#endif
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Sorceror |
Solution 2 | amirtutunchi |
Solution 3 | vandermesis |
Solution 4 | dbDev |
Solution 5 | Yessen Yermukhanbet |