While learning the framework code, it was discovered that modifying the protocol name leads to compilation errors. Below is the test process.
- Create a Swift (command line tool) project in Xcode, including A.swift and B.swift files.
- Compilation error occurs:
B.swift:20:37 Cannot convert value of type 'Data' to expected argument type 'DataResponseSerializer.SerializedObject'
As shown in the figure:
- If the protocol "JsonData" is renamed to "DataJson", recompilation will pass.
Below are the test code files.
A.swift file:
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 file:
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)
}
}
Computer Environment:
macOS 14.2.1 (x86_64)
Xcode 15.3