无法将类型 T.Type 的值转换为 T

Could not cast value of type T.Type to T

我正在尝试使用 AlamofireObjectMapper 创建一个通用方法来发送请求,其中输入是 BasicMappable(通用类型),输出是响应

func sendRequest<T>(endPoint: String,
                    parameters: Parameters,
                    method: HTTPMethod,
                    mappingObject: T.Type) -> T where T : BaseMappable {
        var answer = T.self
        Alamofire.request(applicationBaseURL + endPoint,
                          method: method,
                          parameters: parameters,
                          headers: nil)
            .responseObject { ( response: DataResponse<T>) in
                let data = response.result.value
                answer = data as! T.Type
        }
        return answer as! T
    }

所以我在VC中使用了这样的方法:

let authResponse = requestHelper.sendRequest(endPoint: "/auth/users/create/",
                                             parameters: params,
                                             method: .post,
                                             mappingObject: AnswerClass.self)

而且构建良好。 但是当我尝试发送请求时,我遇到了崩溃和错误

Could not cast value of type 'Travel.AuthController.AnswerClass.Type' (0x110186bf8) to 'Travel.AuthController.AnswerClass' (0x10de153b0).
2019-04-15 18:42:28.811494+0300 Travel[9951:596186] Could not cast value of type 'Travel.AuthController.AnswerClass.Type' (0x110186bf8) to 'Travel.AuthController.AnswerClass' (0x10de153b0).

我已经在 VC 中用这个

完成了
Alamofire.request(applicationBaseURL + "/auth/users/create/",
                                  method: .post,
                                  parameters: params,
                                  headers: nil)
   .responseObject { (response: DataResponse<AnswerClass>) in
                        let loginRespone = response.result.value
                        for error in loginRespone?.errors ?? [] {
                            print(error.detail)
                            print(error.source)
                            print(error.status)
                        }

而且效果很好

但是我想让这个方法对整个项目通用

如何正确地转换这些类型的值,或者还有其他方法吗?

您需要使用下面的completionHandler来获取对象,

func sendRequest<T>(endPoint: String,
                    parameters: Parameters,
                    method: HTTPMethod,
                    mappingObject: T.Type,
                    completion: (T?, Error?) -> Void) {
    Alamofire.request(applicationBaseURL + endPoint,
                      method: method,
                      parameters: parameters,
                      headers: nil)
        .responseObject { ( response: DataResponse<T>) in
            switch response.result {
            case .success(let value):
                completion(value, nil)
            case .failure(let error):
                completion(nil, error)
            }

    }
}

谢谢@Kamran 的帮助!

我最终的解决方案是:

class RequestHelper {
    func sendRequest<T>(endPoint: String,
                        parameters: Parameters,
                        method: HTTPMethod,
                        mappingObject: T.Type,
                        completion: @escaping (_ response: T?, _ error: Error?) -> Void) where T : BaseMappable {
        Alamofire.request(applicationBaseURL + endPoint,
                          method: method,
                          parameters: parameters,
                          headers: nil)
            .responseObject { ( response: DataResponse<T>) in
                switch response.result {
                case .success(let value):
                    completion(value, nil)
                case .failure(let error):
                    completion(nil, error)
                }
        }
    }
}