声明 'subscribe' 不能覆盖多个超类声明 (ReSwift)
Declaration 'subscribe' cannot override more than one superclass declaration (ReSwift)
我在覆盖 ReSwift Pod 中的函数时遇到问题。我有以下模拟 class:
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)
}
这是我的编译器输出
我没有想法所以非常感谢任何帮助
谢谢!
这是您的问题:
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
接口的行为。
我在覆盖 ReSwift Pod 中的函数时遇到问题。我有以下模拟 class:
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)
}
这是我的编译器输出
我没有想法所以非常感谢任何帮助 谢谢!
这是您的问题:
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
接口的行为。