Swift 框架中修改协议名称导致编译错误如何解决?

新手上路,请多包涵

在学习框架代码时,发现修改协议的名称导致编译错误,下面是测试过程
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'
如图:
image.png
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导致呢?

阅读 522
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进