Great answer and explanation, top marks!;) I didn’t really understand the issue originally as I saw ‘Text()’ and ‘Image()’ (and practically everything else in SwiftUI) as of the type ‘View’ since that’s what they return but I guess it’s a little more complicated than that. Come morning I’ll try your suggestion of replacing the generic type with AnyView since it looks more accurate to what I’m trying to do? Honestly never used generics before this, but saw it as a solution to a similar issue/post involving View pass through.
Post
Replies
Boosts
Views
Activity
Yeah I figured it was from the angled brackets.A development related to this post - I had a similar situation but where I actually wanted the List style over ForEach. However this has issues as the Array of pairs doesn't conform to Hashable.As a solution to this and to generally improve code structure I decided to make a stuct to represent the pair. After some playing and research I got the following to work successfully.struct NavLinkPair<NavDestination: View> {
let label: String
let dest: NavDestination
init(_ label:String, _ destination: NavDestination) { // dest param won't use _ once I get it working btw
self.label = label
self.dest = destination
}
}
struct MainMenuCapsule<Content: View>: View {
private let navPair: NavLinkPair<Content>
init(_ navPair: NavLinkPair<Content>) {
self.navPair = navPair
}
// body ...However, when replacing the old "let menuItems: [(label: String, dest: AnyView)] = ..." with the use of the new struct it produced to original heterogeneous errors. As you said in your first reply "As you found, you cannot create an Array of different Views practical enough to use it."Am I just wasting my time pursuing things like this?Bothlet test = [NavLinkPair("Label", Text(""))]
let test2 = [NavLinkPair("Label", Image(somePath))]work fine, but as you'd expect, trying to combine them fails.
Hey, thanks, encapsulating each View inside the array in an "AnyView()" seems to have worked without any side effects!The "NavDestination" was a generic I made to allow View passthrough to MainMenuCapsule - when I copied the code onto here it looks like some items slipped through a bit skewy// original post
struct MainMenuCapsule: View {
// actual code
struct MainMenuCapsule<NavDestination: View>: View {
Yep, the big thing I didn't know when making this post was that SwiftUI was a (relatively new) thing and an alternative to storyboard. I've since decided to get stuck into SwiftUI having found some good resources (including Apple's) as it seems a good investment at the risk of less help and documentation since it's new.