【发布时间】:2017-12-26 12:36:39
【问题描述】:
所以我这里有这个协议
public protocol UseCase {
associatedtype ResponseType
associatedtype Parameters
func build(params: Parameters) -> Single<ResponseType>
}
public extension UseCase {
func execute(params: Parameters) -> Single<ResponseType> {
return build(params: params)
.subscribeOn(ConcurrentDispatchQueueScheduler(qos: DispatchQoS.background))
.observeOn(MainScheduler.instance)
}
}
我有一个像这样实现UseCase 协议的结构
public struct CreateNewAccount: UseCase {
private let repository: AuthRepository
public init(repository: AuthRepository) {
self.repository = repository
}
public func build(params: Params) -> Single<User> {
return repository.register(params: params)
}
public struct Params: RequestParams {
...
}
}
我想在另一个类上使用这个CreateNewAccount,但我不想直接使用CreateNewAccount,我想将它作为UseCase传递,因为它是一个协议,它可以很容易嘲笑测试。
但是当我做这样的事情时
class RegisterViewModel: ViewModel {
private let createNewAccount: UseCase // Error on this line
init(createNewAccount: UseCase) { // Error on this line
self.createNewAccount = createNewAccount
}
}
这给了我这样的错误
Error:(34, 35) protocol 'UseCase' can only be used as a generic constraint because it has Self or associated type requirements
那么,我可以从我的代码中更改一些东西以使这种情况有效吗?提前致谢。
【问题讨论】:
标签: ios swift swift-protocols associated-types