如何扩展ReactiveCocoa4的SignalProducerType

时间:2016-02-20 11:50:42

标签: swift reactive-cocoa

我尝试使用SequenceType值扩展SignalProducerType。但我不能这样做。在编译时出现'表达式类型不明确而没有更多上下文'错误。

protocol TranslatorType {
    typealias Source
    typealias Destination
    func translate(source: Source) -> Destination
}

extension SignalProducerType where Value: SequenceType {
    func translate<T: TranslatorType, U: SequenceType where T.Source == Value.Generator.Element, T.Destination == U.Generator.Element>(translator: T) -> ReactiveCocoa.SignalProducer<U, Error> {
        return lift { $0.map { seq in seq.map(translator.translate) } } # Type of expression is ambiguous without more context error
    }
}

我该怎么做?

1 个答案:

答案 0 :(得分:1)

map SequenceType函数会返回一个数组,您无法映射到通用SequenceType。将类型U更改为数组,它可以编译:

extension SignalProducerType where Value: SequenceType {
    func translate<T: TranslatorType, V where T.Source == Value.Generator.Element, T.Destination == V>(translator: T) -> ReactiveCocoa.SignalProducer<[V], Error> {
        return lift {  $0.map { seq in seq.map(translator.translate) } }
    }
}