15

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.

1 Answer 1

18

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.

2
  • 1
    Had the same problem and could solve it now. Thanks
    – gurehbgui
    Feb 24, 2020 at 10:53
  • My problem seemed similar, but I did not remember to construct my view (parentheses). So it was like I did DataGridPage.tabItem { ... instead of DataGridPage().tabItem { ... In my case, the view did not take any parameters in the constructor.
    – dwaz
    Mar 7, 2020 at 6:11

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.