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
How to fix?
Advanced Practical Enums
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.
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.
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.
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.
I'm adding a more general answer for a few reasons.
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") }
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))) {}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With