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

后端 未结 4 866
你的背包
你的背包 2021-01-31 10:32

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

enum Instagram {
    enum Media {
        case Popular
        case Shortcode(id         


        
4条回答
  •  鱼传尺愫
    2021-01-31 10:53

    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 {
        func request(_ target: Target, completion: @escaping () -> ()) {}
    }
    
    let provider = MoyaProvider()
    provider.request(.Media(.Search(lat: 0, lng: 0, distance: 0))) {}
    

提交回复
热议问题