ccamaisa
ccamaisa

Reputation: 107

navigationBarItems "Type [view] cannot conform to 'View'; only struct/enum/class types can conform to protocols"

I'm trying to apply some navigationBarItems to my View and I've gotten this error - Type '() -> Text' cannot conform to 'View'; only struct/enum/class types can conform to protocols

I've been able to dumb the code down to this. Anyone know what could be causing it?

struct NewEntry: View {

    var body: some View {
        NavigationView {
            VStack {
                Text("Hello World")
            }
            .navigationBarItems(trailing: {
                Text("Hello World")
            })
        }
    }
}

Upvotes: 2

Views: 396

Answers (1)

staticVoidMan
staticVoidMan

Reputation: 20244

.navigationBarItems(trailing:) takes a View.
You are providing the View inside {}

.navigationBarItems(trailing: {
    Text("Hello World")
})

Solution:

.navigationBarItems(trailing: Text("Hello, World"))

SwiftUI's compiler is a bit dumb, small syntax mistakes causes it to throw weird errors at weird places.

Upvotes: 4

Related Questions