在学习框架代码时,发现修改协议的名称导致编译错误,下面是测试过程
1.使用xcode创建 swift (command line tool)项目,包含 A.swift, B.swift 文件
2.编译出现错误:
B.swift:20:37 Cannot convert value of type 'Data' to expected argument type 'DataResponseSerializer.SerializedObject'
如图:
3.如果修改协议 JsonData 名称为 DataJson,重新编译会通过
下面是测试代码文件
A.swift 文件:
import Foundation
public protocol JsonData<SerializedObject> {
associatedtype SerializedObject
func serialize() throws -> SerializedObject
}
public protocol DownloadResp<SerializedObject> {
associatedtype SerializedObject
func serializeDownload() throws -> SerializedObject
}
extension DownloadResp where Self: JsonData {
public func serializeDownload() throws -> Self.SerializedObject {
do {
return try serialize()
} catch {
throw error
}
}
}
public protocol ResponseSerializer<SerializedObject>: JsonData & DownloadResp {
}
public final class DataResponseSerializer: ResponseSerializer {
public func serialize() throws -> Data {
let a: Data = Data()
return a
}
}
B.swift
import Foundation
public final class DownloadRequest {
@discardableResult
public func response_test<Serializer: DownloadResp>(responseSerializer: Serializer,
completionHandler: Serializer.SerializedObject)
-> Self {
return self
}
@discardableResult
public func response_test<Serializer: ResponseSerializer>(responseSerializer: Serializer,
completionHandler: Serializer.SerializedObject)
-> Self {
return self
}
@discardableResult
public func responseData_test(completionHandler: Data) -> Self {
response_test(responseSerializer: DataResponseSerializer(),
completionHandler: completionHandler)
}
}
请问下这个问题是语法编写问题,还是其它原因或bug导致呢?