0
我有一个类StateMachine<A>
如何使用SWIFT协议与泛型方法和泛型类型
final class StateMachine<A> {
private var previousState: State? = nil
private var currentState: State
private var content: A?
var delegate: StateMachineDelegate?
var state: State = .loading {
didSet {
previousState = currentState
currentState = state
}
}
init(currentState: State, delegate: StateMachineDelegate?) {
self.currentState = currentState
}
}
和委托协议StateMachineDelegate
protocol StateMachineDelegate {
func updateWith(content: A)
}
我想表达的是,如果创建的StateMachine对于类型A,代表应该实现方法func updateWith(content: A)
,它接受一个相同类型A的参数。这可能吗?
代码有很多问题,现在:你不能把嵌套类型泛型('枚举State');你必须初始化'init'中的所有非可选属性;你必须在委托协议中指定'associatedtype'。 – user28434