我有以下代码:
protocol ResourceFetchable {
associatedtype ResourceType: Resource
/**
Fetches a resource using the provided service
- parameter resource: The resource to fetch
- parameter service: The service to be used for fetching the resource
*/
func fetch<Service: ResourceService>(resource: ResourceType, usingService service: Service) where Service.ResourceType == ResourceType
}
ResourceFetchable
使用ResourceService
,就像这样 -
protocol ResourceService {
associatedtype ResourceType: Resource
/**
Fetch a resource
- parameter resource: The resource to fetch
- parameter completion: A completion handler called with a Result type of the fetching computation
*/
func fetch(resource: ResourceType, completion: @escaping (Result<ResourceType.Model>) -> Void)
}
而Resource
是一个简单的协议,就像 -
public protocol Resource {
associatedtype Model
}
和Result
定义为 -
public enum Result<T> {
case success(T)
case failure(Error)
}
一切看起来都很好,但编译器会抛出此错误 -
相同类型的约束类型'τ_0_0.ResourceType'不符合 必需的协议'资源'
我已将其缩小为where
的{{1}}方法中的协议约束ResourceFetchable
子句
有任何帮助吗? 提前谢谢。