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

后端 未结 4 873
你的背包
你的背包 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:50

    Enum case Search is not a member of type Instagram

    As the compiler say, Search is not a member of type Instagram. It's just an enum in the scope of Instagram. You have to create a member that is an instance of Search in Instagram

    struct Instagram {
        enum Media {
            case Search(lat: Float, lng: Float, distance: Int)
        }
        // something like:
         var media = .Search(lat: 0, lng: 0, distance: 0) 
        // I'm not sure this one is right syntax
        // because I can't check it right now.
        // please just get the idea
    }
    extension Instagram: TargetType {
        var path: String {
            switch self.media {
            case .Search(let _, let _, let _):
                return "/media/search"
            }
        }
    }
    

提交回复
热议问题