挣扎了一段时间,如果你能阐明这一点,那将会非常有帮助:
我有一个APIWorkerProtocol
它有一个属性要求,所需的属性是一个协议,即DataParserProtocol
protocol APIWorkerProtocol {
var apiRequestProvider : APIRequestGeneratorProtocol {get}
var dataParser : DataParserProtocol{get}
func callAPI(completionHandler: @escaping (APICallResult<Self.ResultType>) -> Void)
}
protocol DataParserProtocol {
associatedtype ExpectedRawDataType
associatedtype ResultType
func parseFetchedData(fetchedData : ExpectedRawDataType) -> APICallResult<ResultType>
}
我怎样才能实现这个目标?
在当前的实现中,这会导致错误Protocol 'DataParserProtocol' can only be used as a generic constraint because it has Self or associated type requirements
.
提前致谢
Ankit
如果协议使用Self
或相关的类型要求(同质协议),我们可能会注意到使用该协议作为具体类型。
所以不要使用DataParserProtocol
作为具体类型dataParser
财产(蓝图在APIWorkerProtocol
),你可以添加一个associatedtype
打字机,说DataParser
,到APIWorkerProtocol
, which 仅限于符合以下条件的类型 the DataParserProtocol
.
另外,我不确定使用的意图是什么Self.ResultType
作为完成处理程序的专业化callAPI(...)
是(因为Self
将指的是实现的类型APIWorkerProtocol
;一个没有蓝图的协议associatedtype
ResultType
): 你的意思是使用ResultType
of the DataParserProtocol
type?
E.g.
protocol APIWorkerProtocol {
associatedtype DataParser: DataParserProtocol
var dataParser : DataParser { get }
func callAPI(completionHandler: @escaping (APICallResult<DataParser.ResultType>) -> Void)
}
protocol DataParserProtocol {
associatedtype ExpectedRawDataType
associatedtype ResultType
func parseFetchedData(fetchedData: ExpectedRawDataType) -> APICallResult<ResultType>
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)