[swift-users] Subclass instance passed to closure expecting parent class “cannot convert value of type to expected argument type”

Kenny Leung kenny_leung at pobox.com
Wed Sep 28 21:37:56 CDT 2016


Could it be because ModelListResponse is Generalized while APIResponse is not?

-Kenny


> On Sep 28, 2016, at 5:43 PM, Eric Conner via swift-users <swift-users at swift.org> wrote:
> 
> Hello all!
> 
> I hope it's not frowned upon to repost a question here from Stack Overflow, but I have not found help there.  In any case, the email gets some nice formatting out of it :-D.
> 
> I am trying to create a subclass with a generic type that can be passed into a completion block, but am getting the following error:
> 
> Cannot convert value of type '(ModelListResponse) -> ()' to expected argument type '(APIResponse) -> ()'
> Parent class:
> 
> import ObjectMapper
> 
> 
> 
> class APIResponse: Mappable {
> 
>     
> var status: String!
> 
>     
> var message: String?
> 
> 
>     required init
> ?(map: Map) {}
> 
> 
>     func mapping
> (map: Map) {
> 
>         status 
> <- map["status"]
> 
>         message 
> <- map["message"]
> 
>     
> }
> }
> Subclass:
> 
> import ObjectMapper
> 
> 
> 
> class ModelListResponse<T: Mappable>: APIResponse {
> 
>     
> var data: ModelList<T>?
> 
> 
>     required init
> ?(map: Map) {
> 
>         
> super.init(map: map)
> 
>     
> }
> 
> 
>     
> override func mapping(map: Map) {
> 
>         
> super.mapping(map: map)
> 
>         data 
> <- map["data"]
> 
>     
> }
> }
> Then I have a function definition:
> 
>  func _GET(path: String, parameters: Parameters, completion:@escaping (APIResponse) -> ())
> And I'm trying to call it like so:
> 
> // error is on this line
> self._GET(path: "/rest/posts", parameters: parameters) { (response: ModelListResponse<Post>) in
> 
>     
> // ...
> }
> But here I get the error about how ModelListResponse cannot be converted. Isn't a ModelListResponse an APIResponse?  I don't understand why this does not work.
> 
> I managed to get around the issue by using a generic. Though I'm not sure why this works and the above code does not. Here is my working declaration:
> 
> func _GET<T: Mappable>(path: String, parameters: Parameters, completion:@escaping (T) -> ()) { }
> Thanks!
> 
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users



More information about the swift-users mailing list