Swift关联类型继承
我想写两个协议,一个是通用协议,另一个是专用协议来处理一些网络请求 对于更通用的Swift关联类型继承,swift,swift-protocols,associated-types,Swift,Swift Protocols,Associated Types,我想写两个协议,一个是通用协议,另一个是专用协议来处理一些网络请求 对于更通用的DORequest协议,所有这些都可以正常工作,但我无法让DOPagedRequest正常工作。这个问题是由associatedtype响应引起的,我想在DOPagedRequest中对其进行更具体的说明 以下是我的协议: public protocol DORequest { associatedtype Response: DOResponse var method: String { get }
DORequest
协议,所有这些都可以正常工作,但我无法让DOPagedRequest
正常工作。这个问题是由associatedtype响应
引起的,我想在DOPagedRequest
中对其进行更具体的说明
以下是我的协议:
public protocol DORequest {
associatedtype Response: DOResponse
var method: String { get }
var path: String { get }
}
public protocol DOResponse: Codable { }
public protocol DOPagedRequest: DORequest where Response: DOPagedResponse {
var page: Int? { get }
var perPage: Int? { get }
}
public protocol DOPagedResponse: Codable {
var links: ResponseLinks { get }
}
public struct ResponseLinks: Codable {
public struct Pages: Codable {
var next: String?
}
public var pages: Pages?
}
及其具体实施示例:
// Implementation of DORequest with no errors
public struct Get: DORequest {
public struct Response: DOResponse {
public let account: String
}
public let method = "GET"
public let path = "account"
public init() { }
}
// Implementation of DOPagedRequest with errors:
// Type 'List' does not conform to protocol 'DORequest'
// Type 'List' does not conform to protocol 'DOPagedRequest'
public struct List: DOPagedRequest {
public var tag: String?
public var page: Int?
public var perPage: Int?
public struct Response: DOPagedResponse {
public var links: ResponseLinks
public let droplets: [String]
}
public let method = "GET"
public let path = "droplets"
public init(tag: String? = nil, page: Int = 0, perPage: Int = 200) {
self.tag = tag
self.page = page
self.perPage = perPage
}
}
可能我遗漏了斯威夫特的关联类型。您忘了从
或响应继承
公共协议DOPagedResponse:DOResponse{
您忘记从或响应继承
公共协议DOPagedResponse:DOResponse{
您的代码中有一些东西对我来说没有多大意义
我不明白的主要一点是反应的性质:
DORequest
中的响应是:associatedtype响应:DOResponse
DOPagedRequest
中的响应是,其中响应:DOPagedResponse
因此,当您声明public struct List:DOPagedRequest
时,编译器将无法确定Response
符合哪种类型
是属于类型或响应类型的响应
,还是属于类型的DOPagedResponse
我建议你做点什么来统一这两个协议,即:DOResponse
和DOPagedResponse
在同一个保护伞下,如下所示:
public protocol GeneralResponse {}
public protocol DOResponse: Codable, GeneralResponse { }
public protocol DOPagedResponse: Codable, GeneralResponse {
var links: ResponseLinks { get }
}
public protocol DORequest {
associatedtype Response: GeneralResponse
var method: String { get }
var path: String { get }
}
public protocol DOPagedRequest {
var page: Int? { get }
var perPage: Int? { get }
}
你的代码中有一些东西对我来说没有多大意义
我不明白的主要一点是反应的性质:
DORequest
中的响应是:associatedtype响应:DOResponse
DOPagedRequest
中的响应是,其中响应:DOPagedResponse
因此,当您声明public struct List:DOPagedRequest
时,编译器将无法确定Response
符合哪种类型
是属于类型或响应类型的响应
,还是属于类型的DOPagedResponse
我建议你做点什么来统一这两个协议,即:DOResponse
和DOPagedResponse
在同一个保护伞下,如下所示:
public protocol GeneralResponse {}
public protocol DOResponse: Codable, GeneralResponse { }
public protocol DOPagedResponse: Codable, GeneralResponse {
var links: ResponseLinks { get }
}
public protocol DORequest {
associatedtype Response: GeneralResponse
var method: String { get }
var path: String { get }
}
public protocol DOPagedRequest {
var page: Int? { get }
var perPage: Int? { get }
}
什么是DO
?它看起来像一个Objective-C前缀。DO代表数字海洋,我正在编写一个API包装,但在Swift中,您可以使用一个模块。您不需要前缀。什么是DO
?它看起来像一个Objective-C前缀。DO代表数字海洋,我正在编写一个API包装,但在Swift中,您可以使用一个模块。您不需要前缀前缀。正如@Jessy所指出的,我忘记了从DOResponse
继承DOResponse
,这基本上就是你所指出的,正如@Jessy所指出的,我忘记了从DOResponse
继承,这基本上也是你所指出的