[英]How to use a protocol with associated type with ObservableObject?
I'm trying to create a protocol for something that must also conform to ObservableObject
, which will live within another object Services
:我正在尝试为还必须符合
ObservableObject
的东西创建一个协议,该协议将存在于另一个 object Services
中:
protocol ThumbnailServiceProtocol: ObservableObject where ObjectWillChangePublisher == ObservableObjectPublisher {
var processingThumbnails: [String] { get }
}
final class ThumbnailService: ThumbnailServiceProtocol {
var processingThumbnails: [String] = []
}
struct Services {
var thumbnailService: ThumbnailServiceProtocol
}
var services = Services(thumbnailService: ThumbnailService())
My problem comes when I try to swap out services to use a mock in my tests like this:当我尝试换出服务以在我的测试中使用模拟时,我的问题就出现了,如下所示:
final class MockThumbnailService: ThumbnailServiceProtocol {
var processingThumbnails: [String] = []
}
services = Services(thumbnailService: MockThumbnailService()) /// Cannot convert value of type 'MockThumbnailService' to expected argument type 'ThumbnailServiceProtocol'
I think the problem is coming from the fact that ObservableObject
is a protocol with an associated type ( associatedtype ObjectWillChangePublisher
) but I thought I'd 'filled in the hole' by specifying where ObjectWillChangePublisher == ObservableObjectPublisher
.我认为问题出在
ObservableObject
是一个具有关联类型( associatedtype ObjectWillChangePublisher
)的协议,但我认为我会通过指定where ObjectWillChangePublisher == ObservableObjectPublisher
来“填补漏洞”。
Am I missing something please?请问我错过了什么吗?
Edit: to clarify, services
is a global object which I'd like to swap out with a new instance of Services
containing mocks in my unit tests, which is why I've tried to make this work:编辑:澄清一下,
services
是一个全局 object ,我想在我的单元测试中用一个包含模拟的新Services
实例来替换它,这就是我试图让它工作的原因:
var services = Services(thumbnailService: ThumbnailService())
services = Services(thumbnailService: MockThumbnailService()) /// Cannot convert value of type 'MockThumbnailService' to expected argument type 'ThumbnailServiceProtocol'
The problem in your example is that Services.processingThumbnails
is of type ThumbnailService
, and not ThumbnailServiceProtocol
.您的示例中的问题是
Services.processingThumbnails
的类型是ThumbnailService
,而不是ThumbnailServiceProtocol
。 These types are not the same.这些类型并不相同。 You need to make
Services
generic.您需要使
Services
通用。 Try this:尝试这个:
protocol ThumbnailServiceProtocol: ObservableObject {
var processingThumbnails: [String] { get }
}
final class ThumbnailService: ThumbnailServiceProtocol {
var processingThumbnails: [String] = []
}
final class MockThumbnailService: ThumbnailServiceProtocol {
var processingThumbnails: [String] = []
}
struct Services<T> where T: ThumbnailServiceProtocol, T.ObjectWillChangePublisher == ObservableObjectPublisher {
var thumbnailService: T
}
let servicesA = Services(thumbnailService: ThumbnailService())
let servicesB = Services(thumbnailService: MockThumbnailService())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.