0

嗨,我对 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()})
        }
    }
}

我收到此错误:

没有“解码”候选产生预期的上下文结果类型“T?”

4

1 回答 1

0

这是由于T关联类型上的泛型不够完整/严格造成的。decode您尝试使用的函数类型指定如下:

func decode<T: Decodable where T == T.DecodedType>(_ object: AnyObject) -> T?

额外的where子句是您的实现中缺少的。将其更改为此应该可以解决它:

func rac_jsonRequest<T : Decodable, G : ErrorType where T == T.DecodedType>(method: Alamofire.Method,_ URLString: URLStringConvertible, parameters: [String: AnyObject]? = nil, encoding: ParameterEncoding = .URL, headers: [String: String]? = nil) -> SignalProducer<T,G> {
于 2016-07-19T17:06:29.693 回答