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