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 CrewMember
is not the same thing as the type MissionView.CrewMember
is not the same thing as the type CrewView.CrewMember
. That's why you get the error message Cannot convert value of type '[MissionView.CrewMember]' to expected argument type '[CrewView.CrewMember]'
Swift is a typesafe language and will be very, very picky about your types. You know and I know that MissionView.CrewMember
and CrewView.CrewMember
describe the same information, but to the compiler they are completely different things.
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 Font
. Defined within Font
are several other nested types, such as Design
, TextStyle
and Weight
. These types only really make sense when used with a Font
, so they are defined within it (as Font.Design
, Font.TextStyle
and Font.Weight
) to scope them to that parent type and indicate that they don't have independent usage.
In the Moonshot example, CrewMember
only has meaning when talking about a mission in the original project (because crew members are constructed in the init
and used in the body
and nowhere outside of MissionView
), so it's defined within MissionView
. However, you are trying to create a separate CrewView
that also needs CrewMember
s. So it would be better to make CrewMember
a standalone type rather than nesting it under another type.
(Alrhough technically you could do this too:
struct CrewView: View {
var crew : [MissionView.CrewMember]
...
}
But I think it would be better to define CrewMember
outside of any particular View
.)