Reputation: 670
I am following this tutorial https://www.youtube.com/watch?v=Xd0J18isFEQ for my app which is a simple app that shows a list of values on the homescreen + then has a detail view for each row
I have implemented a router to dictate the routing for my app as below:
enum Router {
case detail(Result)
}
struct Navigator {
static func navigate<T: View>(_ route: Router, content: () -> T) -> AnyView {
switch route {
case .detail(let item):
return AnyView(NavigationLink(
destination: DetailView(story: item)) {
content()
})
}
}
}
And I am using it on my homescreen embedded like this:
struct Home: View {
@ObservedObject var newsfeed = Newsfeed()
@State var page: Int = 1
var body: some View {
List {
ForEach(newsfeed.data) { item in
Navigator.navigate(.detail(item)) {
HStack {
WebImage(url: URL(string: item.fields.thumbnail)!)
.resizable()
.scaledToFill()
.frame(width: 120, height: 120)
.cornerRadius(2)
}
VStack (alignment: .leading, spacing: 5) {
Text(item.fields.headline)
.font(.headline)
.fontWeight(.bold)
Text(item.fields.trailText)
.font(.caption)
}
.frame(maxHeight: 120)
}
.onAppear() {
if (newsfeed.data.last == item) {
newsfeed.loadData(pageParam: page + 1, search: nil, key: nil) { result, size in
if (result != nil) { newsfeed.data.append(contentsOf: result!) }
if (size != nil) { newsfeed.pageSize += size! }
}
self.page += 1
}
}
}
}
}
}
But I am getting an error on Navigator.navigat.. Type '()' cannot conform to View, only struct/enum/class types can conform to protocols
Upvotes: 0
Views: 278
Reputation: 52337
navigate
, as written, wants to take one argument, but you're sending it both an HStack
and a VStack
.
@ViewBuilder
can solve this for you, which is an annotation (used often in SwiftUI, especially in the built-in components) that lets the view have/return multiple root-level elements:
static func navigate<T: View>(_ route: Router, @ViewBuilder content: () -> T) -> AnyView {
Upvotes: 2