在 class 函数中调用 Alamofire 时出错
Getting an error when calling the Alamofire in a class function
我正在使用 Alamofire 获取 data.URLRequestConvertible 协议用于构造 request.When 通过 Alamofire 在请求的 class 函数中调用 URLRequestConvertible 枚举 我收到类似这样的错误(从“(AFDataResponse) throws -> Void”类型的抛出函数(又名“(DataResponse) throws -> ()”)到非抛出函数类型“(AFDataResponse) -> Void”的无效转换(又名“(DataResponse) -> ()')”。在我正在获取结果的请求函数中,我如何使用泛型?
Q1:获取结果时出错
Q2:我如何在函数中使用泛型
URLRequestConvertible 枚举:
enum Router: URLRequestConvertible{
case getAllDishes
var bseUrl : URL{
return URL(string: "https://yummie.glitch.me/")!
}
var method: HTTPMethod{
switch self {
default:
return .get
}
}
var path:String{
switch self{
case .getAllDishes:
return "dish-categories"
}
}
func asURLRequest() throws -> URLRequest {
let url = bseUrl.appendingPathComponent(path)
var request = URLRequest(url: url)
request.method = method
return request
}
}
正在调用请求的函数:
class NetworkLayer{
class func requested(_ request:Router,completion:@escaping(Result<Data,Error>) -> Void){
ProgressHUD.show() //if response comes that loader run
AF.request(request).responseJSON{ (response) in
switch response.result{
case .success(let data):
do{
let getDishesData = data as? [String:Any]
let resp = try JSONSerialization.data(withJSONObject: getDishesData?["data"], options: .prettyPrinted)
completion(.success(response))
}
case .failure(let error):
completion(.failure(error))
}
}
}
您需要添加 Do-Catch 语句
catch – 如果抛出方法失败并抛出错误,执行将落入此catch块。
class NetworkLayer{
class func requested(_ request:Router,completion:@escaping(Result<Data,Error>) -> Void){
ProgressHUD.show() //if response comes that loader run
AF.request(request).responseJSON{ (response) in
switch response.result{
case .success(let data):
do{
let getDishesData = data as? [String:Any]
let resp = try JSONSerialization.data(withJSONObject: getDishesData?["data"], options: .prettyPrinted)
completion(.success(response))
}catch{
print(error)
completion(.failure(error))
}
case .failure(let error):
completion(.failure(error))
}
}
}
}
这里再给你一个建议,不需要做JSONSerialization,因为responseJSON给你直接的响应(Alamofire会做JSONSerialization)。
最终代码
class NetworkLayer{
class func requested(_ request:Router,completion:@escaping(Result<Data,Error>) -> Void){
ProgressHUD.show() //if response comes that loader run
AF.request(request).responseJSON{ (response) in
switch response.result{
case .success(let response):
do{
print(response)
completion(.success(response))
}catch{
print(error)
completion(.failure(error))
}
case .failure(let error):
completion(.failure(error))
}
}
}
}
我正在使用 Alamofire 获取 data.URLRequestConvertible 协议用于构造 request.When 通过 Alamofire 在请求的 class 函数中调用 URLRequestConvertible 枚举 我收到类似这样的错误(从“(AFDataResponse) throws -> Void”类型的抛出函数(又名“(DataResponse
URLRequestConvertible 枚举:
enum Router: URLRequestConvertible{
case getAllDishes
var bseUrl : URL{
return URL(string: "https://yummie.glitch.me/")!
}
var method: HTTPMethod{
switch self {
default:
return .get
}
}
var path:String{
switch self{
case .getAllDishes:
return "dish-categories"
}
}
func asURLRequest() throws -> URLRequest {
let url = bseUrl.appendingPathComponent(path)
var request = URLRequest(url: url)
request.method = method
return request
}
}
正在调用请求的函数:
class NetworkLayer{
class func requested(_ request:Router,completion:@escaping(Result<Data,Error>) -> Void){
ProgressHUD.show() //if response comes that loader run
AF.request(request).responseJSON{ (response) in
switch response.result{
case .success(let data):
do{
let getDishesData = data as? [String:Any]
let resp = try JSONSerialization.data(withJSONObject: getDishesData?["data"], options: .prettyPrinted)
completion(.success(response))
}
case .failure(let error):
completion(.failure(error))
}
}
}
您需要添加 Do-Catch 语句
catch – 如果抛出方法失败并抛出错误,执行将落入此catch块。
class NetworkLayer{
class func requested(_ request:Router,completion:@escaping(Result<Data,Error>) -> Void){
ProgressHUD.show() //if response comes that loader run
AF.request(request).responseJSON{ (response) in
switch response.result{
case .success(let data):
do{
let getDishesData = data as? [String:Any]
let resp = try JSONSerialization.data(withJSONObject: getDishesData?["data"], options: .prettyPrinted)
completion(.success(response))
}catch{
print(error)
completion(.failure(error))
}
case .failure(let error):
completion(.failure(error))
}
}
}
}
这里再给你一个建议,不需要做JSONSerialization,因为responseJSON给你直接的响应(Alamofire会做JSONSerialization)。
最终代码
class NetworkLayer{
class func requested(_ request:Router,completion:@escaping(Result<Data,Error>) -> Void){
ProgressHUD.show() //if response comes that loader run
AF.request(request).responseJSON{ (response) in
switch response.result{
case .success(let response):
do{
print(response)
completion(.success(response))
}catch{
print(error)
completion(.failure(error))
}
case .failure(let error):
completion(.failure(error))
}
}
}
}