Search code examples
swiftswiftuiprotocolsswiftui-navigationlink

How to declare a protocol with a function that returns NavigationLink


I want to declare a protocol that has function which must return NavigationLink. But when I try this it returns an error "Reference to generic type 'NavigationLink' requires arguments in <...>"

protocol Protocol: class{
    func function() -> NavigationLink
}

(Jessy)

class BeersListRouter: BeersListRouterProtocol{
    typealias Label = Text
    typealias Destination = View
    
    func getBeerDetailsView(for beer: Beer) -> NavigationLink<Label, Destination>{
        
    }
}

Solution

  • NavigationLink is a generic type, with two placeholders. You need to account for them.

    protocol Protocol: AnyObject {
      associatedtype Label: View
      associatedtype Destination: View
      
      func function() -> NavigationLink<Label, Destination>
    }