|
I think I am misunderstanding something basic about passing variables that are structs between views, would appreciate any help! I am trying to break out the crew horizontal scroll view from the MissionView in Moonshot into a separate view. MissionView defines a struct called CrewMember as
and populates a variable 'crew' with an init as an array of CrewMember. So to break a ScrollView out into a separate view I am defining a CrewView as
When I pass CrewView(crew), though, I get an error:
I've tried eliminating the struct in CrewView, re-initializing the whole thing in CrewView, etc. But nothing works. I think my logic is right for how this should work but perhaps I'm missing something fundamental about how structs work and get passed between views? Thanks in advance for any help |
|
If you are going to share So, instead of:
you would have:
|
|
So, just to make sure I understand, the struct isnt just defining a type of data structure its creating something more like an object? Since those structs are each children of different parents they arent the same thing? I've been thinking of structs as a data structure definition rather than as something more akin to class. Am I still missing it? |
|
No, you're right, structs are data structure definitions that you instantiate as instances of those definitions. The problem here is that in Swift you can define nested types. A type defined inside another type is different from a type of the same name defined outside of an parent type and is different from a type nested inside a different parent type. IOW, the type Sometimes nested types are defined as such because they are only used inside some other type. Sometimes nested types are defined as such as a sort of type of namespacing. You see the latter a lot in Apple's frameworks. For instance, in SwiftUI there is a type called In the Moonshot example, (Alrhough technically you could do this too:
But I think it would be better to define |
|
Makes so much sense when you describe it that way ;) -- thank you so much for your help. I was really confused and this has really helped me to understand structs. I really appreciate it! |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.