swift - How do you use a switch statement with a nested enum? -


i'm getting error on switch statement below path.

enum case search not member of type instagram

enum instagram {     enum media {         case popular         case shortcode(id: string)         case search(lat: float, lng: float, distance: int)     }     enum users {         case user(id: string)         case feed         case recent(id: string)     } } extension instagram: targettype {     var path: string {         switch self {         case .media.search(let _, let _, let _):             return "/media/search"         }     } } 

i want use switch statement return path, cases giving me errors. there way work?

this using advanced enums: https://github.com/terhechte/appventure-blog/blob/master/resources/posts/2015-10-17-advanced-practical-enum-examples.org#api-endpoints

i'm adding more general answer few reasons.

  1. this open question regarding nested enums , switch statements. the other 1 sadly closed.
  2. the legit answer not show how assign value of nested enum symbol. syntax not intuitive me.
  3. none of other answers have extensive case examples.
  4. an enum nested 3 levels deep more illustrative of required syntax. using efremidze answer above still took me while work out.

an example:

enum action {     enum f {         enum {             case fail             case success         }         enum d {             case fail             case success         }         enum h {             case none             case         }         case attack(a)         case defend(d)         case hurt(h)     }     enum w {         case swing         case     }     case fighter(f)     case weapon(w) }  // matches "3 deep" let action = action.fighter(.attack(.fail)) // matches "1 deep" because more general case listed first. let action2 = action.weapon(.swing)  switch action { case .fighter(.attack(.fail)):     print("3 deep") case .weapon:     print("1 deep") case .weapon(.swing):     print("2 deep case") case .fighter(.attack):     print("2 deep enum level") default:     print("wtf enum") } 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -