There's a number of problems with your example:
- a struct cannot return value - you'll need a method (or computed property) for this.
- you're referencing an undefined enum
time
that comes from nowhere, so it's not possible to tell if the switch
is exhaustive.
- you need to return an instance of a type, not just the type name.
- you are returning two different types, which would only work if they both conform to, and you return, the same protocol, or if you change them to classes and return a parent class.
The contrived example below tries to demonstrate these concepts, which may help you research them, although I'm not sure of its utility!
protocol MyProtocol {
// protocol definition
}
struct StructA: MyProtocol {
// struct contents and protocol conformance
}
struct StructB: MyProtocol {
// struct contents and protocol conformance
}
struct MainStruct {
enum TimePeriod {
case morning
case afternoon
}
func structFor(timePeriod time: TimePeriod) -> MyProtocol {
switch(time) {
case .morning:
return StructB()
case .afternoon:
return StructA()
}
}