为Alamofire和Argo创建反应式扩展

时间:2016-06-14 19:14:24

标签: swift swift2 alamofire reactive-cocoa argo

嗨,我对Alamofire的反应性扩展有问题

这是:

extension Alamofire.Manager{

    func rac_jsonRequest<T : Decodable, G : ErrorType>(method: Alamofire.Method,_ URLString: URLStringConvertible, parameters: [String: AnyObject]? = nil, encoding: ParameterEncoding = .URL, headers: [String: String]? = nil) -> SignalProducer<T,G> {

        SignalProducer<T,G> { sink, disposable in
            let request = self.request(method, URLString, parameters: parameters, encoding: encoding, headers: headers).responseJSON { response in
                if let JSON = response.result.value{
                    if let decoded : T = decode(JSON){ //ERROR THIS
                        sink.sendNext(decoded)
                        sink.sendCompleted()
                    }
                }
            }
            disposable.addDisposable({request.cancel()})
        }
    }
}

我收到此错误:

  

否&#39;解码&#39;候选人产生预期的上下文结果类型&#39; T?&#39;

1 个答案:

答案 0 :(得分:0)

这是由于通用ServicePointManager.MaxServicePointIdleTime = 1; 在关联类型上不够完整/严格造成的。您尝试使用的T函数类型如下所示:

decode

额外的func decode<T: Decodable where T == T.DecodedType>(_ object: AnyObject) -> T? 子句是您的实现中缺少的。将其更改为此应修复它:

where