【问题标题】:Passing class type to generic将类类型传递给泛型
【发布时间】:2016-05-19 19:38:30
【问题描述】:

在下面的代码中,我如何将一个类传递给 responseType ?

func sendGetRequest(url:String,
                   request: String,
                   responseType: AnyClass,
                  completion: (AnyObject?, ServerError?)-> Void) {
Alamofire.request(.GET, url, parameters: nil, encoding: .JSON, headers: headers)
      .validate()
      .responseObject { (response: Response<**responseType**, NSError>) in

...

}

目前 AnyClass 不工作它说“responseType 不是类型” 如果我将 responseType 替换为实际的类,它就可以工作。但我想让函数通用,并且 responseType 可以是任何类以正确映射。

【问题讨论】:

    标签: ios swift alamofire objectmapper


    【解决方案1】:

    如果这是您尝试使用的方法:

    https://github.com/Alamofire/Alamofire/search?utf8=%E2%9C%93&q=responseObject

    那么问题是 AnyClass 不符合 ResponseObjectSerializable,您可以扩展 AnyClass 以包含 ResponseObjectSerializable 所需的方法,或者您可以让您的方法参数采用 ResponseObjectSerializable 而不是 anyClass

    【讨论】:

    • 好的,我明白了。我错过了对 ResponseObjectSerializable 部分的确认。谢谢。
    猜你喜欢
    • 2016-02-12
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    相关资源
    最近更新 更多