【发布时间】:2020-05-07 10:05:24
【问题描述】:
我想编写两种协议,一种是通用的,另一种是专门用于处理一些网络请求的。
对于更通用的DORequest 协议,一切正常,但我无法让DOPagedRequest 工作。问题是由associatedtype Response 引起的,我想在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
}
}
我可能遗漏了一些关于 Swift 的关联类型的信息。
【问题讨论】:
-
什么是
DO?它看起来像一个 Objective-C 前缀。 -
DO 代表 Digital Ocean,我正在编写 API 包装器????
-
但是在 Swift 中,你可以使用模块。你不需要前缀。
标签: swift swift-protocols associated-types