Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 参数类型为associatedtype的Swift协议_Ios_Swift_Swift Protocols_Associated Types - Fatal编程技术网

Ios 参数类型为associatedtype的Swift协议

Ios 参数类型为associatedtype的Swift协议,ios,swift,swift-protocols,associated-types,Ios,Swift,Swift Protocols,Associated Types,我有这个协议 public protocol UseCase { associatedtype ResponseType associatedtype Parameters func build(params: Parameters) -> Single<ResponseType> } public extension UseCase { func execute(params: Parameters) -> Single<

我有这个协议

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)
    }

}
我想在另一个类上使用这个
CreateNewAccount
,但我不想直接使用
CreateNewAccount
,我想把它作为
用例来传递,因为它是一个协议,可以很容易地模拟测试。
但是当我做这样的事情时

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 {
        ...
    }
}
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
这给了我一个这样的错误

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 {
        ...
    }
}
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

那么,我的代码中是否有一些东西可以更改,以使这种情况有效?提前感谢。

您不能将关联类型的协议用作字段

您只能将它们用作类的实现。在大多数情况下,这些类应该是泛型的

例如,此代码是允许的:

public struct CreateNewAccount<T, K>: UseCase {

    public typealias ResponseType = T

    public typealias Parameters = K
}
公共结构CreateNewAccount:UseCase{ 公共类型别名ResponseType=T 公共类型别名参数=K }
所以,

private let createNewAccount:createNewAccount


或者用另一种方式包装。

嗨,谢谢你的回答。这意味着我必须直接对其消费者使用
CreateNewAccount
?你有别的办法来对付这个案子吗?因此,我可以将抽象(协议)类型传递给使用者,而不是具体的实现。正如我所知,如果只想使用协议,唯一的解决方案是使用另一个协议<代码>公共结构CreateNewAccount:UseCase,YourNewSimpleProtocol