CaspD3V
CaspD3V

Reputation: 31

SwiftUI Bug Fix "Cannot convert value of type 'Binding<int>'to expected type 'Binding<_>?'"

I'm having trouble with this error :

"Cannot convert value of type 'Binding'to expected type 'Binding<_>?'"

I think I'm coding in an out dated version of SwiftUI but I'm not 100% sure, so any help I can get will be awesome.

I displayed my code down below so you can take a look at it.

I'm not sure if the bottom part matters but I added it just to be safe.

ContentView

import SwiftUI

struct ContentView: View {
    @State private var selection = 0
    @State var networkManager = NetworkManager()
    var body: some View {
        TabView(selection: $selection){

            NavigationView{
            Text("First View")
                .font(.title)
                    .navigationBarTitle(Text("Welcome"))
                }
                .tabItem {
                    VStack {
                        Image(systemName: "star.fill")
                        Text("Welcome")
                    }
                }
                .tag(0)
            NavigationView{

                List(networkManager.featureList.results.identified(by: \.url)) { featured in
                Text(featured.name.capitalized)
                }
                    .navigationBarTitle(Text("Featured"))
                }
                .tabItem {
                    VStack {
                        Image(systemName: "app.badge.fill")
                        Text("Featured")
                    }
                }
                .tag(1)
            NavigationView{
            Text("First View")
                .font(.title)
                    .navigationBarTitle(Text("Repos"))
                }
            .tabItem {
                VStack {
                    Image(systemName: "rectangle.stack.fill")
                    Text("Repos")
                }
            }
            .tag(2)
            NavigationView{
            Text("First View")
                .font(.title)
                    .navigationBarTitle(Text("Request"))
                }
            .tabItem {
                VStack {
                    Image(systemName: "icloud.and.arrow.down.fill")
                    Text("Request")
                }
            }
            .tag(3)
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

ApiView

import Foundation
import SwiftUI
import Combine

class NetworkManager: ObservableObject {
    var didChange = PassthroughSubject<NetworkManager, Never>()
    var featureList = FeaturedApiList(results: []){
        didSet{
            didChange.send(self)
        }
    }

    init(){
        guard let url = URL(string: "https://myurl.com/repos.json") else { return }
        URLSession.shared.dataTask(with: url) { (data, _, _) in
            guard let data = data else { return }
            let featureList = try! JSONDecoder().decode(FeaturedApiList.self, from: data)
            DispatchQueue.main.async {
                self.featureList = featureList

            }
        }.resume()
    }
}

Thanks!

Upvotes: 2

Views: 2686

Answers (2)

Glenn Posadas
Glenn Posadas

Reputation: 13283

Welcome to Stackoverflow!

OOPer is actually correct. You will need to fix your List. Let's try to replace your data for the List just to see that the project will compile:

Suppose we have a model that conforms to Identifiable protocol, like so:

struct Person: Identifiable {
    var id = UUID()
    var name: String
}

And then replace your List line with this:

List([Person(name: "fafa")]) { featured in
    Text(featured.name)
}

This time it should run. Since SwiftUI is new, this must be the reason why the error is quite confusing.

Upvotes: 0

OOPer
OOPer

Reputation: 47876

The error shown is very confusing, but some other errors would cause this sort of error.

In your case, you may need to fix the second NavigationView in the TabView:

            NavigationView{
                //↓Fix this line.
                List(networkManager.featureList.results, id: \.url) { featured in
                    Text(featured.name.capitalized)
                }
                .navigationBarTitle(Text("Featured"))
            }
            .tabItem {
                VStack {
                    Image(systemName: "app.badge.fill")
                    Text("Featured")
                }
            }
            .tag(1)

Better check this thread and always try to find an up-to-date samples or tutorials.

Upvotes: 2

Related Questions