gpt4 book ai didi

swift - 使用泛型组合 2 个独立的几乎相同的函数

转载 作者:行者123 更新时间:2023-11-28 07:34:29 28 4
gpt4 key购买 nike

我有 2 个函数用于在一些网络代码上提供一个层。

历史上只有 1 个函数,但我刚刚坐下来扩展它并意识到除了某些类型之外它们是相同的。

是否可以使用泛型将它们结合起来?

或者是过度杀戮?

我读了一些书,正在努力理解如何在这里实现泛型

func profile(with endpoint: ProfilesEndpoint, method: HTTPMethod, body: String?, headers: [String: String]?, completion: @escaping (Either<ProfileResponse>) -> Void) {
var request = endpoint.request
request.httpMethod = method.rawValue

if let body = body {
request.httpBody = body.data(using: .utf8)
}

if let headers = headers {
for (key, value) in headers {
request.addValue(value, forHTTPHeaderField: key)
}
}

execute(with: request, completion: completion)
}

func identity(with endpoint: IdentityEndpoint, method: HTTPMethod, body: String?, headers: [String: String]?, completion: @escaping (Either<OAuthTokenResponse>) -> Void) {
var request = endpoint.request
request.httpMethod = method.rawValue

if let body = body {
request.httpBody = body.data(using: .utf8)
}

if let headers = headers {
for (key, value) in headers {
request.addValue(value, forHTTPHeaderField: key)
}
}

execute(with: request, completion: completion)
}

我已经尝试过,但收到一个错误,提示 Value of type 'T' has no member 'request'

func request<T: Comparable, X: Comparable>(with endpoint: T, method: HTTPMethod, body: String?, headers: [String: String]?, completion: @escaping (Either<X>) -> Void) {
var request = endpoint.request
request.httpMethod = method.rawValue

if let body = body {
request.httpBody = body.data(using: .utf8)
}

if let headers = headers {
for (key, value) in headers {
request.addValue(value, forHTTPHeaderField: key)
}
}

execute(with: request, completion: completion)
}

最佳答案

你得到那个错误是因为不是所有类型都有这个名为 request 的变量.泛型不适合这个问题,因为你指的是一个特定的类(它不是通用的)。

这里有两个想法可以解决您的问题:

方法1-

我认为这是最好的选择,因为它很直观 - 每个函数都有自己的目的和描述,同时代码循环使用并保持简单

改变

func request<T: Comparable, X: Comparable>(with endpoint: T, method: HTTPMethod, body: String?, headers: [String: String]?, completion: @escaping (Either<X>) -> Void)

private func request(urlRequest: URLRequest, method: HTTPMethod, body: String? , headers: [String: String]?, completion: @escaping (response: HttpResponse) -> Void)

然后从profile(...)identity(...)你可以调用你的请求函数-

func profile(with endpoint: ProfilesEndpoint, method: HTTPMethod, body: String?, headers: [String: String]?, completion: @escaping (Either<ProfileResponse>) -> Void) {
var urlRequest = endpoint.request
// call request(urlRequest: urlRequest, ...) and handle completion
}

func identity(with endpoint: IdentityEndpoint, method: HTTPMethod, body: String?, headers: [String: String]?, completion: @escaping (Either< OAuthTokenResponse>) -> Void) {
var urlRequest = endpoint.request
// call request(urlRequest: urlRequest, ...) and handle completion
}

private func request(urlRequest: URLRequest, method: HTTPMethod, body: String? , headers: [String: String]?, completion: @escaping (response: HttpResponse) -> Void){

var request = urlRequest
.
.
.
}

方法2-

创建一个名为 Endpoint 的协议(protocol),并在 ProfileEndpoint 和 IdentityEndpoint 上实现它。

protocol Endpoint{
var request: URLRequest { get }
}

class ProfilesEndpoint: Endpoint {
// this class must contain variable request because it implements Endpoint protocol
var request: URLRequest{
.
.
return ...
}
}
class IdentityEndpoint: Endpoint {
// this class must contain variable request because it implements Endpoint protocol
var request: URLRequest{
.
.
return ...
}
}

然后你可以把你的请求函数改成-

(我真的不熟悉Either<>所以我不确定你的完成情况)

func request(with endpoint: Endpoint, method: HTTPMethod, body: String?, headers: [String: String]?, completion: @escaping (Either<X>) -> Void) {

var request = endpoint.request
request.httpMethod = method.rawValue

if let body = body {
request.httpBody = body.data(using: .utf8)
}

if let headers = headers {
for (key, value) in headers {
request.addValue(value, forHTTPHeaderField: key)
}
}

execute(with: request, completion: completion)
}

您可以使用 IdentityEndpoint 调用此函数或 ProfilesEndpoint因为它们都符合 protocol Endpoint - 谁声明了变量request ,所以可以肯定的是,这两个类也会有这个变量。

关于swift - 使用泛型组合 2 个独立的几乎相同的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53579293/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com