声明'subscribe'不能覆盖多个超类声明(ReSwift)

时间:2018-01-25 11:35:51

标签: ios swift reswift

从ReSwift Pod覆盖函数时遇到问题。我有以下模拟类:

import Foundation
import Quick
import Nimble
import RxSwift
@testable import MainProject
@testable import ReSwift

    class MockReSwiftStore: ReSwift.Store<MainState> {
    var dispatchDidRun: Bool = false
    var subscribeWasTriggered: Bool = false

    init() {
        let reducer: Reducer<MainState> = {_, _  in MainState() }
        super.init(reducer: reducer, state: nil)
    }

    required init(
        reducer: @escaping (Action, State?) -> State,
        state: State?,
        middleware: [(@escaping DispatchFunction, @escaping () -> State?) -> (@escaping DispatchFunction) -> DispatchFunction]) {
        super.init(reducer: reducer, state: state, middleware: middleware)
    }

    override func subscribe<SelectedState, S>(
        _ subscriber: S,
        transform: ((Subscription<MainState>) -> Subscription<SelectedState>)?)
        where S: StoreSubscriber,

        S.StoreSubscriberStateType == SelectedState {
            subscribeWasTriggered = true
        }
    }
}

当覆盖订阅方法时,我遇到了错误

enter image description here

然后当使用自动完成时,它还显示2次出现: enter image description here

然而,当寻找原始功能时,只有一个看起来像这样

open func subscribe<SelectedState, S: StoreSubscriber>(
    _ subscriber: S, transform: ((Subscription<State>) -> Subscription<SelectedState>)?
) where S.StoreSubscriberStateType == SelectedState
{
    // Create a subscription for the new subscriber.
    let originalSubscription = Subscription<State>()
    // Call the optional transformation closure. This allows callers to modify
    // the subscription, e.g. in order to subselect parts of the store's state.
    let transformedSubscription = transform?(originalSubscription)

    _subscribe(subscriber, originalSubscription: originalSubscription,
               transformedSubscription: transformedSubscription)
}

这是我的编译器输出 enter image description here

我没有想法,所以非常感谢任何帮助 谢谢!

1 个答案:

答案 0 :(得分:2)

以下是您的问题:

class Some<T> {

    func echo() {
        print("A")
    }

}

extension Some where T: Equatable {

    func echo() {
        print("B")
    }

}


class AnotherSome: Some<String> {

    override func echo() {
        print("Doesn't compile")
    }

}

问题是:ReSwift开发人员将Store.subscribe行为声明为接口的一部分并作为扩展的一部分(我不确定他们为什么选择这样做而不是引入其他对象)。 Swift无法弄清楚你试图覆盖哪个部分,因此它无法编译。 Afaik没有语言工具可以帮助您解决这个问题。

一种可能的解决方案是将MockStore实现为StoreType并使用Store对象来实现StoreType接口的行为。