[swift-users] Subclass instance passed to closure expecting parent class “cannot convert value of type to expected argument type”
Eric Conner
ericwconner at gmail.com
Wed Sep 28 19:43:42 CDT 2016
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 lineself._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!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160928/0f2c7ae4/attachment.html>
More information about the swift-users
mailing list