Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've created an enum for Instagram endpoints with nested enums similar to Moya.

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)     } } 

I would like to return the path for each endpoint.

extension Instagram: TargetType {     var path: String {         switch self {         case .Media.Shortcode(let id):             return "/media/shortcode"         }     } } 

However I'm getting an error on the switch statement above for the path.

Enum case Shortcode is not a member of type Instagram

How to fix?

Advanced Practical Enums

like image 569
efremidze Avatar asked Jan 16 '16 00:01

efremidze


People also ask

Can you use a switch statement around an enum?

An Enum keyword can be used with if statement, switch statement, iteration, etc. enum constants are public, static, and final by default. enum constants are accessed using dot syntax. An enum class can have attributes and methods, in addition to constants.

Can we use enum in switch case in C?

We can use enums in C for multiple purposes; some of the uses of enums are: To store constant values (e.g., weekdays, months, directions, colors in a rainbow) For using flags in C. While using switch-case statements in C.

Can Swift enumerations nested?

To accomplish this, Swift enables you to define nested types, whereby you nest supporting enumerations, classes, and structures within the definition of the type they support. To nest a type within another type, write its definition within the outer braces of the type it supports.

Can enum be nested?

Nested Enum TypesWe can use any of the access modifiers (public, private, protected, or package) level for a nested enum type. The following code shows how to declare a nested public enum type named Gender inside a Person class.


2 Answers

I'm adding a more general answer for a few reasons.

  1. This is the only open question regarding nested enums and switch statements. The other one is sadly closed.
  2. The only legit answer does not show how to assign the value of a nested enum to a symbol. The syntax was not intuitive to me.
  3. None of the other answers have extensive case examples.
  4. An enum nested 3 levels deep is more illustrative of the required syntax. Using efremidze answer still took me a while to work it out.
 enum Action {     case fighter(F)     case weapon(W)      enum F {         case attack(A)         case defend(D)         case hurt(H)          enum A {             case fail             case success         }         enum D {             case fail             case success         }         enum H {             case none             case some         }     }     enum W {         case swing         case back     } }  // 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 to case") case .fighter(.attack):     print("2 deep to another enum level") default:     print("WTF enum") } 
like image 144
Jeff Avatar answered Sep 23 '22 22:09

Jeff


By adding an associated value for the nested enum you can access it using a switch statement.

enum Instagram {     enum MediaEndpoint {         case Search(lat: Float, lng: Float, distance: Int)     }     case Media(MediaEndpoint) }  extension Instagram: TargetType {     var path: String {         switch self {         case .Media(.Search):             return "/media/search"         }     } }  // Demo  protocol TargetType {     var path: String { get } }  class MoyaProvider<Target: TargetType> {     func request(_ target: Target, completion: @escaping () -> ()) {} }  let provider = MoyaProvider<Instagram>() provider.request(.Media(.Search(lat: 0, lng: 0, distance: 0))) {} 
like image 20
efremidze Avatar answered Sep 24 '22 22:09

efremidze