从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
}
}
}
当覆盖订阅方法时,我遇到了错误
然而,当寻找原始功能时,只有一个看起来像这样
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)
}
我没有想法,所以非常感谢任何帮助 谢谢!
答案 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
接口的行为。