I'm trying to determine the destination of a NavigationLink of SwiftUI dynamically within init() based on the generic data provided to the view.
Based on the code below, if the Item is a Tour the destination should be a TourDetailView and otherwise a LocationDetailView.
struct CardView<Item: CardItem>: View {
let card: Item
private let destination: T
init(card: Item) {
self.card = card
if Item.self == Tour.self {
destination = TourDetailView(tour: card as! Tour) as! T
} else {
destination = LocationDetailView(location: card as! Location) as! T
}
}
var body: some View {
NavigationLink(destination: destination) {
...
}
}
}
Right now the code is not compiling because T isn't defined. If I define T as:
struct CardView<Item: CardItem, T: View>: View {
CardView is handled but then I have to edit the View calling the CardView which both introduces unnecessary complexity and eventually I have to define T in @main which is not allowed.
How can I achieve what I want? I tried to make destination another type but with no success.