我有一个带有通用类型的类型。我正在尝试使它成为另一种类型的属性,但不知道如何使类型别名实现通用类型。
这就是我想要做的:
protocol StoreType: AnyObject {
associatedtype State: StateType
func send(_ action: State.Action)
}
struct MainPresenter {
typealias Store = StoreType where Store.State == MainState
private let store: Store
init(store: Store) {
self.store = store
}
}
这给了我'where' clause cannot be attached to a non-generic declaration
的编译错误。正确的方法是什么?
答案 0 :(得分:1)
MainPresenter
与Store
通用。 Store
必须在类型定义中。
您可以为其加上模块名称的前缀,因此无需使用某些Type
后缀。 (Type
是旧约定。use Protocol
now是必要的人,因为不可能进行阴影处理。)
protocol Store: AnyObject {
associatedtype State: Module.State
func send(_ action: State.Action)
}
struct MainPresenter<Store: Module.Store> where Store.State == MainState {
private let store: Store
init(store: Store) {
self.store = store
}
}