Alamofire无法在当前上下文中推断闭包类型

时间:2018-05-29 11:20:38

标签: ios swift alamofire

我正在为DataRequest创建扩展以记录事件

func log() -> Self {
        let responseSerializer = DataRequest.jsonResponseSerializer(options: .allowFragments)
        return response(queue: nil, responseSerializer: responseSerializer) { response in
            if let object = response.result.value {
                /* do the job */
            }
        }
}

并收到错误Unable to infer closure type in the current context 由于它具有(DataResponse<T.SerializedObject>) -> Void泛型类型,有没有办法显式设置类型来修复此错误?

Alamofire版本: 4.7.2 Xcode版本: 9.3 Swift版本: 4 运行Alamofire的平台: 的iOS

1 个答案:

答案 0 :(得分:0)

func log() -> Self {
        let responseSerializer = DataRequest.jsonResponseSerializer()
        return response(responseSerializer: responseSerializer) { [weak self] response in
            guard let _self = self else { return }
            _self.printRequestString(response)
        }
    }

效果很好。