功能如下:

func registerFor<Element>(relayId id: String) -> Driver<Element>? {
    guard let relay = relays[id] as? BehaviorRelay<Element> else { return nil }
    return relay.asObservable()
        .distinctUntilChanged { a, b in
            return a != b
        }.flatMapLatest { value in
            return Observable.create { observer in
                observer.on(.next(value))
                return Disposables.create()
            }
        }.asDriver(onErrorJustReturn: Element())
}

distinctUntilChanged行引发以下错误:
Contextual closure type '(Element) throws -> _' expects 1 argument,
but 2 were used in closure body

asDriver行抛出以下错误(当然):
Non-nominal type 'Element' does not support explicit initialization

Context:我有一个类,理想情况下它有各种类型(字符串、int等)的BehaviorRelays集合。Element通常代表这些类型,但这会产生两个问题:
distinctUntilChanged坚持使用闭包(例如:如果此方法返回Driver<String>则只需使用distinctUntilChanged()即可,但泛型Element使其抱怨缺少闭包);
onErrorJustReturn需要一个具体的值,但Element是通用的。
下面的“解决方法”可能有用,但我怀疑有更好的解决方法
protocol Inii {
    init()
}

func registerFor(relayId id: String, def: Inii.Type) -> Driver<Inii>? {
    return relays[id]?.asObservable()
        .distinctUntilChanged { _, _ in
            return true
        }.flatMapLatest { value in
            return Observable.create { observer in
                observer.on(.next(value))
                return Disposables.create()
            }
        }.asDriver(onErrorJustReturn: def.init())
}

尽管我仍然不确定该在distinctUntilChanged结束语中加入什么。
附录A
我认为,如果要为非泛型类型实现distinctUntilChanged闭包,需要执行以下操作:
.distinctUntilChanged { previousValue, currentValue in
    return previousValue == currentValue
}

但是,当与泛型Element一起使用时,仍会引发以下错误:
Contextual closure type '(Inii) throws -> _' expects 1 argument,
but 2 were used in closure body

附录B
下面是另一个有点不同问题的选择:
protocol Inii {
    init()
}

var relay = BehaviorRelay<String>(value: "")

func registerFor<Element>(def: Element.Type) -> Driver<Element> where Element: Inii {
    return relay.asObservable()
        .distinctUntilChanged { previousValue, currentValue in
            return previousValue == currentValue
        }.flatMapLatest { value in
            return Observable.create { observer in
                observer.on(.next(value))
                return Disposables.create()
            }
        }.asDriver(onErrorJustReturn: def.init())
}

这种情况下的错误是:
Member 'next' in 'Event<_>' produces result of type 'Event<Element>',
but context expects 'Event<_>'

observer.on

最佳答案

只要distinctUntilChanged()符合Element的要求,就可以使用Equatable而不关闭:

protocol EmptyInit {
    init()
}

func registerFor<Element>(relayId id: String) -> Driver<Element>? where Element: Equatable, Element: EmptyInit {
    guard let relay = relays[id] as? BehaviorRelay<Element> else { return nil }
    return relay.asObservable()
        .distinctUntilChanged()
        .flatMapLatest { value in
            return Observable.create { observer in
                observer.on(.next(value))
                return Disposables.create()
            }
        }.asDriver(onErrorJustReturn: Element())
}

10-06 05:20