Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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_":

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__.

Unknown preview provider "ContentView_Previews__

Is anyone else coming across this? My Xcode version is Version 12.3 (12C33).

like image 401
aheze Avatar asked Jan 24 '21 17:01

aheze


2 Answers

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".

like image 156
Sorceror Avatar answered Oct 17 '22 07:10

Sorceror


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.

like image 5
amirtutunchi Avatar answered Oct 17 '22 07:10

amirtutunchi