Moya: [help] how to using pattern matching??

Created on 13 Dec 2018  路  4Comments  路  Source: Moya/Moya

how to using pattern matching??
in this case => case API_some2(let dict), API_some4(let dict) ??

enum SomeService {
  case API_some1
  case API_some2([String: Any])
  case API_some3
  case API_some4([String: Any])
}

extension SomeService {
  public var baseURL: URL { return URL(string: "http://someapi.com") }

  public var path: String { return "path" }

  public var method: Moya.Method { return .post }

  public var task: Task {
    switch self {
      /// how to using pattern matching??
      /// in this case => case API_some2(let dict), API_some4(let dict) ??
      /// 
    case _(let dict):
      return .requestParameters(parameters: dict, encoding: .httpBody)

    default:
      return .requestParameters(parameters: [:], encoding: .httpBody)
    }
  }
}
question

All 4 comments

Hey 馃槂

You should be able to do the following:

switch self {
  case .API_some1, .API_some3:
    print(self)
  case .API_some2(let dictionary), .API_some4(let dictionary):
   print(self, dictionary)
}

Does that help?
I don't think you can match "any case that has an associated type".

@BasThomas thanx.

The answer has already been applied

but a lot of similar api. How can I remove duplicates?

switch self {
  case .API_some1, .API_some3:
    print(self)
  case .API_some2(let dictionary), .API_some4(let dictionary), 
           ...
           ...
           .API_some_n(let dictionary)
:
   print(self, dictionary)
}

I tried the following but it failed.

switch self {
  case .API_some1, .API_some3:
    print(self)

  default:
    guard case _ (let dict) = self else {
        return
    }
   // or
    guard case let (dict) = self, dict is [String: Any] else {
        return
    } 

   return .requestParameters(parameters: dict, encoding: .httpBody)
}

I (unfortunately) don't think you can do that.

@basThomas thanx.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fenixsolorzano picture fenixsolorzano  路  3Comments

fenixsolorzano picture fenixsolorzano  路  3Comments

hamada147 picture hamada147  路  3Comments

ghost picture ghost  路  3Comments

GurpalRajput picture GurpalRajput  路  3Comments