简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM