2
votes

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")
            })
        }
    }
}
1

1 Answers

3
votes

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