gpt4 book ai didi

swift - Alamofire Swift 转换

转载 作者:行者123 更新时间:2023-11-30 11:33:05 24 4
gpt4 key购买 nike

嗨,我想向 API 发出请求,但是发送时,控制台显示给我

invalidURL

 Alamofire.request("https://.../api/v1.8/set/order/?address=\(address)&email=\(email)&information=\(information)&name=\(name)&order=\(parameters)&password=\(password)&paymentType=\(paymentType)&phone=\(phone)&token=\(token)&userID=\(userID)&wihtRegistration=\(wihtRegistration)").validate(statusCode: 200..<300)
.responseJSON { response in
switch response.result
{
case .failure(let error):
print(error)

case .success(let value):
print(value)

print("Request: \(response.request)")

}
}

如何在 Alamofire 中进行转换?

最佳答案

我创建了这个自定义方法。通过传递所需参数来调用它:-

没有 JSON 编码

func requestWithoutJSONEncoding(_ method: HTTPMethod
, _ URLString: String
, parameters: [String : AnyObject]? = [:]
, headers: [String : String]? = [:]
, completion:@escaping (Any?) -> Void
, failure: @escaping (Error?) -> Void) {

Alamofire.request(URLString, method: method, parameters: parameters, headers: headers)
.responseJSON { response in

switch response.result {
case .success:
completion(response.result.value!)
case .failure(let error):
failure(error)
}
}
}

使用 JSON 编码

func request(_ method: HTTPMethod
, _ URLString: String
, parameters: [String : AnyObject]? = [:]
, headers: [String : String]? = [:]
, completion:@escaping (Any?) -> Void
, failure: @escaping (Error?) -> Void) {

Alamofire.request(URLString, method: method, parameters: parameters, encoding: JSONEncoding.default, headers: headers)
.responseJSON { response in

switch response.result {
case .success:
completion(response.result.value!)
case .failure(let error):
failure(error)
}
}
}

关于swift - Alamofire Swift 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50037899/

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