Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert value of type 'Binding<Int>' to expected argument type 'Binding<_>'

Tags:

swiftui

I'm trying to create a TabView in SwiftUI with following code:

@State var selection = 0

var body: some View {
    TabView(selection: $selection) {
        DataGridPage(type: "media").tabItem {
            Image(systemName: "photo.on.rectangle")
                .imageScale(.large)
                .foregroundColor(.yellow)
        }
        .tag(1)

        DataGridPage(type: "files").tabItem {
            Image(systemName: "doc.on.doc")
                .imageScale(.large)
                .foregroundColor(.yellow)
        }
        .tag(2)
    }
}

But I'm receiving error Cannot convert value of type 'Binding<Int>' to expected argument type 'Binding<_>'. I see that the variable selection is integer, which is the correct type but the warning is still there for some reason.

like image 317
atulkhatri Avatar asked Nov 06 '19 06:11

atulkhatri


1 Answers

I figured out the problem. The thing is that TabView shows this error even if there is some error in the closure. So the code of creating the TabView is correct but the problem is the way I'm initialising DataGridPage. I changed the name of the property type to data inside DataGridPage but here I'm still using type property. I fixed it and it stopped showing me the warning.

I think SwiftUI is a new framework and it has still a lot improvement to do in terms of debugging. I hope it will mature in future and we would be able to pin point the exact error instead of this vague statement.

The new code now looks like this:

@State var selection = 0

var body: some View {
    TabView(selection: $selection) {
        DataGridPage(data: "media").tabItem {
            Image(systemName: "photo.on.rectangle")
                .imageScale(.large)
                .foregroundColor(.yellow)
        }
        .tag(1)

        DataGridPage(data: "files").tabItem {
            Image(systemName: "doc.on.doc")
                .imageScale(.large)
                .foregroundColor(.yellow)
        }
        .tag(2)
    }
}

Hope it helps someone facing similar problem.

like image 180
atulkhatri Avatar answered Nov 27 '22 13:11

atulkhatri