'Type of expression is ambiguous without more context with Enum return
I need to pass class type to dynamically decode JSON according to the chosen value in Enum.
Below is an example I made to illustrate (the commented snippet that is showing an error):
class MyModel1: Codable {
let foo: Bool
private enum CodingKeys: String, CodingKey {
case foo = "foo"
}
}
class MyModel2: Codable {
let foo: Int
private enum CodingKeys: String, CodingKey {
case foo = "foo"
}
}
enum ApiType {
case status
case register
var responseType: AnyObject.Type {
switch self {
case .status:
return MyModel1.self
case .register:
return MyModel2.self
}
}
}
enum ResponseType {
case MyModel1
case MyModel2
}
enum Result<T, E: Error> {
case success(T)
case failure(E)
}
typealias Handler = (Result<Data, Error>) -> Void
func request<T>(responseType: T.Type, completion: @escaping Handler) where T: Decodable, T: Encodable {
print(responseType)
}
//request(responseType: ApiType.status.responseType) { result in
// switch result {
// case .success(let response):
// print(response)
// case .failure(let error):
// print(error)
// }
//}
request(responseType: MyModel1.self) { result in
switch result {
case .success(let response):
print(response)
case .failure(let error):
print(error)
}
}
print(ApiType.status.responseType === MyModel1.self)
When I try to run the commented part I get the error: Type of expression is ambiguous without more context
But, as you can see in the comparison I made at the end of the code, the types are exactly the same.
What could be causing this problem?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
