简体   繁体   English

如何将具有关联类型的协议与 ObservableObject 一起使用?

[英]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.

相关问题 如何使用 Swinject 注册符合 ObservableObject 的协议? - How to use Swinject to register protocol conforming to ObservableObject? 如何使用带有关联类型的协议使用类型擦除 - How can I use Type Erasure with a protocol using associated type 如何使用关联类型协议作为函数的参数类型? - How to use associated type protocol as argument's type to a function? 具有关联类型的Swift协议:如何在抽象方法中使用? - Swift protocol with associated type: how to use in an abstract method? 如何使用具有相关类型的协议数组定义协议 - How to define a protocol with an array of a protocol with an associated type 在Swift中将受约束的关联类型的协议用作属性 - Use protocol with constrained associated type as property in Swift 如何使协议关联类型需要协议继承而不是协议采用 - How to make protocol associated type require protocol inheritance and not protocol adoption 如何使用 UIApplicationDelegateAdaptor 作为 ObservableObject? - How to use UIApplicationDelegateAdaptor as ObservableObject? 如何将 ObservableObject 与 UIViewRepresentable 一起使用 - How to use ObservableObject with UIViewRepresentable 使用从另一个协议继承的协议作为关联类型 - Use protocol that inherits from another protocol as an associated type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM