Preview Autogenerated Code Error

Hello,

Very recently, the following code has automatically appeared at the bottom of three of my SwiftUI View files:

@available(iOS 17.0, macOS 14.0, tvOS 17.0, visionOS 1.0, watchOS 10.0, *)
struct $s10Accent_Ace33_0BADA584A03144EFDAB57154E6FD3FBALl7PreviewfMf_15PreviewRegistryfMu_: DeveloperToolsSupport.PreviewRegistry {
    static let fileID: String = "Accent_Ace/HistoryView.swift"
    static let line: Int = 47
    static let column: Int = 1

    static func makePreview() throws -> DeveloperToolsSupport.Preview {
        DeveloperToolsSupport.Preview {
            let randomWord1 = FetchWord.getRandomWord()
            let randomWord2 = FetchWord.getRandomWord()
            @State var randomWords = [Word(word: randomWord1.0, IPA: randomWord1.1, lineNumber: randomWord1.2), Word(word: randomWord2.0, IPA: randomWord2.1, lineNumber: randomWord2.2)]
            HistoryView(words: $randomWords)
        }
    }
}

This is from one of my files but it's very similar in the other two. It seems to have something to do with my previews.

The problem is that this code generates an error: Ambiguous use of 'init(_:traits:body:)'. My previews worked just fine before the auto-generated code appeared, so I tried deleting it. However, it automatically comes back no matter how many times I get rid of it. It's preventing me from building my App Playground, so I can't run the app or even see the previews. Does anyone know how to get rid of it or fix the error?

Thanks for the help!

I believe you are seeing the expansion of a #Preview macro, which can occur if there is a problem in your code that ends up causing a syntax error in the macro expansion (note you can view the macro expansion of a working macro by control clicking it and choosing "Show Macro Expansion" in the context menu). Is there a #Preview directly above this? What does it look like?

While it would not lead to a syntax error, it is worth pointing out that using @State inline is currently not supported.

One more thing I missed in my initial reading of the macro expansion. You likely need a return keyword before HistoryView in your #Preview body.

Thank you for your reply. Here's what my preview looks like. It's right above the expansion of the #Preview macro. I just added the return keyword right before HistoryView(words: $randomWords), but the problem persists.

#Preview {
    let randomWord1 = FetchWord.getRandomWord()
    let randomWord2 = FetchWord.getRandomWord()
    @State var randomWords = [Word(word: randomWord1.0, IPA: randomWord1.1, lineNumber: randomWord1.2), Word(word: randomWord2.0, IPA: randomWord2.1, lineNumber: randomWord2.2)]
    return HistoryView(words: $randomWords)
}

What do you mean by "@State inline"? Does that mean I can't declare a ``@State` variable in my preview? Sorry, I am new to app development.

@Mikachupichu I think that's it, yes. @State variables are supposed to be defined in Views.

You could create a constant binding for your preview like this:

#Preview {
    let randomWord1 = FetchWord.getRandomWord()
    let randomWord2 = FetchWord.getRandomWord()
    let randomWords = [Word(word: randomWord1.0, IPA: randomWord1.1, lineNumber: randomWord1.2), Word(word: randomWord2.0, IPA: randomWord2.1, lineNumber: randomWord2.2)]
    return HistoryView(words: .constant(randomWords))
}

You won't be able to edit the randomWords from HistoryView in Previews, but it should be suitable for previewing your view in SwiftUI Previews.

Otherwise, if you really need to edit the value from within Previews, you could create a wrapper View like this:

#Preview("HistoryView Preview with Wrapper") { // I like giving names to custom previews in order to make them easy to find in Xcode :)
    struct WrapperView: View {
        let randomWord1 = FetchWord.getRandomWord()
        let randomWord2 = FetchWord.getRandomWord()
        @State let randomWords = [Word(word: randomWord1.0, IPA: randomWord1.1, lineNumber: randomWord1.2), Word(word: randomWord2.0, IPA: randomWord2.1, lineNumber: randomWord2.2)]
        var body: some View {
            HistoryView(words: $randomWords)
        }
    }
    return WrapperView()
}
Preview Autogenerated Code Error
 
 
Q