相同类型约束类型'τ_0_0.AssociatedType'不符合所需协议'ProtocolName'

时间:2016-11-15 17:59:56

标签: ios swift swift3 swift-protocols

我有以下代码:

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子句

有任何帮助吗? 提前谢谢。

0 个答案:

没有答案