I have two enumerations:
public enum ServerState { case Connecting case Open case LoggedIn case Closed case Error(NSError) } enum TransportLayerState { case Disconnected(NSError?) case Connecting case Connected }
I need to switch between them if I return "false" if ServerState is set to a state that is not possible in the base state of TL. For example, it will return false if serverState == .Open && tlState == .Disconnected . I try to do this using the switch statement, but I believe that I really want to match the case where one of the states is not a specific state. For instance:
switch (serverState, tlState) { case (.Connecting, !.Connecting): return false case (.Closed, !.Disconnected(.None)): return false case (.Error(_), !.Disconnected(.Some(_)): return false case (.Open, !.Connected), (.LoggedIn, !.Connected): return false default: return true }
Obviously this does not work because you cannot fit ! before the case statement. My only option is to indicate all valid cases that are much more numerous. It makes sense to specify restrictions and allow all other combinations of states, but I'm not sure how to do this. Any ideas? I am also trying to avoid nested switch statements.
Note. I know that I can do this with Swift 2 if case , but right now I cannot use Swift 2, as this is production code. So please answer the solutions in Swift 1.2.
source share