How can I make a swift struct return another struct based on a switch statement? I need it to work like the so: if the time is afternoon then MainStruct will return StructA, and if the time is morning, MainStruct will return StructB. I tried implementing it like this:
struct MainStruct {
switch(time) {
case .morning:
return StructB
case .afternoon:
return StructA
}
}
but I get this error on the switch statement: "Expected declaration".
There's a number of problems with your example:
timethat comes from nowhere, so it's not possible to tell if theswitchis exhaustive.The contrived example below tries to demonstrate these concepts, which may help you research them, although I'm not sure of its utility!