AF 没有成员 'responseDecodable' 有路由器
AF no member 'responseDecodable' with router
请帮忙解释一下为什么路由器没有responseDecodable。我为 AF 请求调用制作了一个路由器,其中一个端点需要向上发送 String: [String: Any]
。我不确定我做错了什么。谢谢!
AFRouter
enum AFRouter: URLRequestConvertible {
case test([String: [String: Any]])
var base: URL {
return URL(string: "https://example.com")!
}
var method: HTTPMethod {
switch self {
case .test:
return .get
}
var path: String {
switch self {
case .test(_):
return "/v2/test"
}
}
func asURLRequest() throws -> URLRequest {
let urlString = baseURL.appendingPathComponent(path).absoluteString.removingPercentEncoding!
let removeSpace = urlString.replacingOccurrences(of: " ", with: "")
let url = URL(string: removeSpace)
var request = URLRequest(url: url!)
request.method = method
switch self {
case .test(_):
guard let token = defaults.string(forKey: "token") else {
return request
}
request.setValue("Bearer " + token , forHTTPHeaderField: "Authorization")
request = try JSONEncoding.default.encode(request)
return request
}
}
可编码
struct Test: Codable {
let success: String
let message: String
let data: [String]
}
呼叫API
func getTest(testInfo: [String: Any]) {
AF.request(AFRouter.test(["Testing": testInfo]).responseDecodable(of: Test.self) { response in //got error here "Value of type 'AFRouter' has no member 'responseDecodable'"
//do something...
})
}
错误是说您想在 AFRouter
实例上使用 responseDecodable(of:)
。
但是,实际上,您想在 DataRequest
个实例上使用它。
但它“应该可以工作”,所以您是在错误的实例上调用它吗?如果我们观察,缺少 )
:
AF.request(AFRouter.test(["Testing": testInfo]).responseDecodable(of:...
=>
AF.request(AFRouter.test(["Testing": testInfo])).responseDecodable(of:...
请帮忙解释一下为什么路由器没有responseDecodable。我为 AF 请求调用制作了一个路由器,其中一个端点需要向上发送 String: [String: Any]
。我不确定我做错了什么。谢谢!
AFRouter
enum AFRouter: URLRequestConvertible {
case test([String: [String: Any]])
var base: URL {
return URL(string: "https://example.com")!
}
var method: HTTPMethod {
switch self {
case .test:
return .get
}
var path: String {
switch self {
case .test(_):
return "/v2/test"
}
}
func asURLRequest() throws -> URLRequest {
let urlString = baseURL.appendingPathComponent(path).absoluteString.removingPercentEncoding!
let removeSpace = urlString.replacingOccurrences(of: " ", with: "")
let url = URL(string: removeSpace)
var request = URLRequest(url: url!)
request.method = method
switch self {
case .test(_):
guard let token = defaults.string(forKey: "token") else {
return request
}
request.setValue("Bearer " + token , forHTTPHeaderField: "Authorization")
request = try JSONEncoding.default.encode(request)
return request
}
}
可编码
struct Test: Codable {
let success: String
let message: String
let data: [String]
}
呼叫API
func getTest(testInfo: [String: Any]) {
AF.request(AFRouter.test(["Testing": testInfo]).responseDecodable(of: Test.self) { response in //got error here "Value of type 'AFRouter' has no member 'responseDecodable'"
//do something...
})
}
错误是说您想在 AFRouter
实例上使用 responseDecodable(of:)
。
但是,实际上,您想在 DataRequest
个实例上使用它。
但它“应该可以工作”,所以您是在错误的实例上调用它吗?如果我们观察,缺少 )
:
AF.request(AFRouter.test(["Testing": testInfo]).responseDecodable(of:...
=>
AF.request(AFRouter.test(["Testing": testInfo])).responseDecodable(of:...