SwiftUI - "Type of Expression is ambiguous without more context" on Text() call

559 views Asked by At

I'm currently trying to pass an array of values into a detail view using a navigation link. However, whenever I attempt to pass the array, the text function call I specify to display the navigation link gives me the "Type of Expression is ambiguous without more context" error. It's not a problem when I'm passing simple, single values, but I need to be able to display every element in the array in the detail view.

struct Item: Identifiable{
           let id=UUID()
           let name:String
    }

    struct Group: Identifiable{
            let id:Int
            let thing:String
            let items:[Item]
    }

    //the "Item" objects are currently placeholders.
    var groups = [
        Group(id: 0,
              thing:"Tops",
              items:[Item(name:"first"), Item(name:"second"), Item(name:"third")]),
        Group(id: 2,
              thing:"Jeans",
              items:[Item(name:"first"), Item(name:"second"), Item(name:"third")]),
        Group(id: 3,
              thing:"Skirts",
              items:[Item(name:"first"), Item(name:"second"), Item(name:"third")]),
        Group(id: 4,
              thing:"Shoes",
              items:[Item(name:"first"), Item(name:"second"), Item(name:"third")]),
        Group(id: 5,
              thing:"Pajamas",
              items:[Item(name:"first"), Item(name:"second"), Item(name:"third")]),
        Group(id: 6,
              thing:"Jackets",
              items:[Item(name:"first"), Item(name:"second"), Item(name:"third")]),
        Group(id: 7,
              thing:"Hats",
              items:[Item(name:"first"), Item(name:"second"), Item(name:"third")]),
        Group(id: 8,
              thing:"Jewelry",
              items:[Item(name:"first"), Item(name:"second"), Item(name:"third")])
    ]

var body: some View {
        List(groups){ group in
            NavigationLink(destination : ClosetDetailView(items: group.items)){
                        //location of error
                        Text(group.thing)
                }
            }
        .background(Color.pinkish)
        .navigationBarTitle("Categories")
        }
}

struct PlaceholderView:View {
    var body:some View{
        Text("placeholder")
    }
}

struct ClosetDetailView:View {
    struct Item: Identifiable{
              let id=UUID()
              let name:String
    }

    var items:[Item]

    var body : some View{
        List(items){ item in
            HStack{
                Text(item.name)
            }
        }
    }
}

1

There are 1 answers

1
Victor Sanchez On

First of all, to enable navigation to a detail view you should embed your list inside a NavigationView:

struct ContentView: View {
    var body: some View {
        NavigationView {
            List(groups) { group in
                NavigationLink(destination: ClosetDetailView(items: group.items)) {
                    Text(group.thing)
                    .listRowBackground(Color.red)
                }
            }
            .navigationBarTitle("Categories")
        }

    }
}

We usually get the error "Type of expression is ambiguous without more context" if there is an error when passing the arguments to a function. In this case, your closet detail view was expecting an array of Item. The problem is that you defined an Item Struct inside ClosetDetailView and outside of it. This error when passing the items to ClosetDetailView might cause the whole NavigationLink to get an error. Delete the Struct inside ClosetDetailView and the error goes away:

struct ClosetDetailView:View {
    var items:[Item]

    var body : some View{
        List(items){ item in
            HStack{
                Text(item.name)
            }
        }
    }
}