skip to Main Content

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

6

Answers


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

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
  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.

    Login or Signup to reply.
  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
    
    Login or Signup to reply.
  6. Must uncheck Pin Preview!

    I got the error Unknown Preview Provider after renaming a view and its’ preview. The error Unknown preview provider included the old name even though it didn’t even exist anymore since I had renamed both the view and the preview.

    If the Pin Preview is selected when renaming the Preview
    this error will be shown, so make sure it is not checked or justuncheck it and check it again and the error will go away.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search