atulkhatri
atulkhatri

Reputation: 11343

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

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.

Upvotes: 16

Views: 29000

Answers (1)

atulkhatri
atulkhatri

Reputation: 11343

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.

Upvotes: 23

Related Questions