【问题标题】:Declaration 'subscribe' cannot override more than one superclass declaration (ReSwift)声明“订阅”不能覆盖多个超类声明 (ReSwift)
【发布时间】:2018-01-25 11:35:50
【问题描述】:

我在从 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
        }
    }
}

当覆盖订阅方法时,我收到以下错误

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

但是在寻找原始函数时,只有一个看起来像这样

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)
}

这是我的编译器输出

我没有想法,因此非常感谢任何帮助 谢谢!

【问题讨论】:

    标签: ios swift reswift


    【解决方案1】:

    这是你的问题:

    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 接口的行为。

    【讨论】:

    • 问题是,你不能使用StoreType作为特定类型,因为里面有关联类型
    • @VladHatko 类型擦除它
    • 是的,但对于测试 - 我真的不知道你打算如何使用它。用MockStore 替换Store,你必须使用类型为StoreType 的var,因为associatedtype,你不能这样做。这有意义吗?
    • @VladHatko 这就是类型擦除的重点。您必须使用MyStoreType 子类型StoreType,这将添加一个func toAnyStore() -&gt; AnyStoreType&lt;YourAssociatedType&gt; 并创建一个类AnyStoreType&lt;YourAssociatedType&gt;,它采用origin 和隧道StoreType 方法并将其用作整个应用程序的依赖项。跨度>
    • @VladHatko 这就是 RxSwift 的例子
    猜你喜欢
    • 2014-08-19
    • 1970-01-01
    • 2013-06-03
    • 2016-08-27
    • 1970-01-01
    • 1970-01-01
    • 2018-06-28
    • 1970-01-01
    • 2017-08-22
    相关资源
    最近更新 更多