gpt4 book ai didi

swift - 如何扩展ReactiveCocoa4的SignalProducerType

转载 作者:行者123 更新时间:2023-11-30 10:06:46 25 4
gpt4 key购买 nike

我尝试使用 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
}
}

我怎样才能做到这一点?

最佳答案

SequenceTypemap 函数返回一个数组,您无法映射到通用 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) } }
}
}

关于swift - 如何扩展ReactiveCocoa4的SignalProducerType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35522830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com