为什么 associatedtype 不符合其约束协议中定义的协议?
Why does associatedtype not conform to protocols defined in its constraining protocol?
我正在尝试让我的 SwiftUI 视图更“可预览”,因此我正在使它们在其商店 (ViewModel) 上通用,这样我就可以更轻松地模拟它们。
考虑以下示例:
public protocol HomeViewStore: ObservableObject {
associatedtype AnimatableImageStoreType = AnimatableImageStore
var title: String { get }
var animatableImageStores: [AnimatableImageStoreType] { get }
var buttonTapSubject: PassthroughSubject<Void, Never> { get }
var retryTapSubject: PassthroughSubject<Void, Never> { get }
}
public protocol AnimatableImageStore: ObservableObject, Identifiable {
var imageConvertible: Data? { get }
var onAppear: PassthroughSubject<Void, Never> { get }
}
struct AnimatableImage<
AnimatableImageStoreType: AnimatableImageStore
>: View {
@ObservedObject private var store: AnimatableImageStoreType
public init(store: AnimatableImageStoreType) {
self.store = store
}
...
}
public struct HomeView<
HomeViewStoreType: HomeViewStore
>: View {
@StateObject private var store: HomeViewStoreType
public init(store: HomeViewStoreType) {
self._store = StateObject(wrappedValue: store)
}
public var body: some View {
VStack {
Text(store.title)
Button {
store.buttonTapSubject.send(())
} label: {
Text("API Call")
}
.background(Color(.accent))
.padding()
Button {
store.retryTapSubject.send(())
} label: {
Text("Retry")
.font(.monserrat(.bold, 14))
}
.padding()
List(store.animatableImageStores) { animatableImageStore in
AnimatableImage(store: animatableImageStore)
}
}
.background(Color(.accent))
}
}
该代码给我以下错误消息:
我的问题是,为什么 HomeViewStoreType.AnimatableImageStoreType 在 HomeViewStore 协议中不符合 AnimatableImageStore 协议,我将其限制为 AnimatableImageStore 协议,而 AnimatableImageStore 符合的 Identifiable 也是如此?
如果有人能告诉我实现此目标的正确方法,我将不胜感激:)
此行不将 AnimatableImageStoreType
约束为 AnimatableImageStore
:
associatedtype AnimatableImageStoreType = AnimatableImageStore
将 =
替换为 :
即可:
associatedtype AnimatableImageStoreType: AnimatableImageStore
我正在尝试让我的 SwiftUI 视图更“可预览”,因此我正在使它们在其商店 (ViewModel) 上通用,这样我就可以更轻松地模拟它们。
考虑以下示例:
public protocol HomeViewStore: ObservableObject {
associatedtype AnimatableImageStoreType = AnimatableImageStore
var title: String { get }
var animatableImageStores: [AnimatableImageStoreType] { get }
var buttonTapSubject: PassthroughSubject<Void, Never> { get }
var retryTapSubject: PassthroughSubject<Void, Never> { get }
}
public protocol AnimatableImageStore: ObservableObject, Identifiable {
var imageConvertible: Data? { get }
var onAppear: PassthroughSubject<Void, Never> { get }
}
struct AnimatableImage<
AnimatableImageStoreType: AnimatableImageStore
>: View {
@ObservedObject private var store: AnimatableImageStoreType
public init(store: AnimatableImageStoreType) {
self.store = store
}
...
}
public struct HomeView<
HomeViewStoreType: HomeViewStore
>: View {
@StateObject private var store: HomeViewStoreType
public init(store: HomeViewStoreType) {
self._store = StateObject(wrappedValue: store)
}
public var body: some View {
VStack {
Text(store.title)
Button {
store.buttonTapSubject.send(())
} label: {
Text("API Call")
}
.background(Color(.accent))
.padding()
Button {
store.retryTapSubject.send(())
} label: {
Text("Retry")
.font(.monserrat(.bold, 14))
}
.padding()
List(store.animatableImageStores) { animatableImageStore in
AnimatableImage(store: animatableImageStore)
}
}
.background(Color(.accent))
}
}
该代码给我以下错误消息:
我的问题是,为什么 HomeViewStoreType.AnimatableImageStoreType 在 HomeViewStore 协议中不符合 AnimatableImageStore 协议,我将其限制为 AnimatableImageStore 协议,而 AnimatableImageStore 符合的 Identifiable 也是如此?
如果有人能告诉我实现此目标的正确方法,我将不胜感激:)
此行不将 AnimatableImageStoreType
约束为 AnimatableImageStore
:
associatedtype AnimatableImageStoreType = AnimatableImageStore
将 =
替换为 :
即可:
associatedtype AnimatableImageStoreType: AnimatableImageStore