Please note that func responseA is a static type, only applicable to the API space of the JSON you paste. Therefore, we need a generic type for all APIs.
So we can delete func responseA and define a protocol MYRequest, like this:
protocolMYRequest{ var router: String { get } var api: String { get }
associatedtypeT: Decodable// generics type }
extensionMYRequest{ var api: String { return"v1" }
var domain: String { return"https://domain.com" }
// T is used in DataResponse<T> funcrequest(completionHandler: @escaping (DataResponse<T>) -> Void) { let url ="\(domain)/\(api)/\(router)" Alamofire.request(url).responseDecodable(completionHandler: completionHandler) } }
Now, we can implement different requests as needed:
1 2 3 4 5 6 7 8 9
structARequest: MYRequest{ let router ="a" typealiasT=A }
structBRequest: MYRequest{ let router ="b" typealiasT=B }
Additionally, we can define another protocol V2 with the same protocol var api: String { get } in MYRequest if we have a new CRequest and the API is upgraded to v2:
1 2 3 4 5 6 7 8 9 10 11
protocolV2{ var api: String { get } } extensionV2{ var api: String { return"v2" } }
structC: Request, V2{ let router ="c" typealiasT=C }
Finally, the requests in the ViewController will look like this:
1 2 3 4 5 6 7 8 9 10
ARequest().request { [weakself] response in iflet a = response.result.value { // a is A, not Any
} } BRequest().request { [weakself] response in iflet b = response.result.value { // b is B, not Any