在这种情况下,“ HTTPMethod”对于类型查找是不明确的?

时间:2019-09-04 02:37:02

标签: swift alamofire

  

有人可以解决这个问题吗?我正在使用Swift 5和Xcode 10.3

     

以下代码的创建是为了避免重复代码。   所以我创建了一个新函数来请求带有传递参数的服务器   在下面。

func requestServer(_ method: HTTPMethod,_ path: String,_ params: [String: Any]?,_ encoding:
    ParameterEncoding,_ completionHandler: @escaping (JSON) -> Void) {

    let url = baseURL?.appendingPathComponent(path)

    refreshTokenIfNeed {

        AF.request(url!, method: method, parameters: params, encoding: encoding, headers: nil).responseJSON{ response in

            switch response.result {
            case .success(let value):
                let jsonData = JSON(value)
                completionHandler(jsonData)
                break


            case .failure:
                completionHandler(nil)
                break

            }
        }
    }

}

// API - Getting Restaurant list
func getRestaurant(completionHandler: @escaping (JSON) -> Void) {

    let path = "api/customer/restaurants"
    requestServer(.get, path, nil, URLEncoding(), completionHandler)


}

// API - Getting list of Meal for Restaurants
func getMeals(restauranId: Int, completionHandler: @escaping (JSON) -> Void) {

    let path = "api/customer/meals/\(RestaurantId)"
    requestServer(.get, path, nil, URLEncoding(), completionHandler)

2 个答案:

答案 0 :(得分:0)

在调用函数之前使用此功能

var request = URLRequest(url: url)
request.httpMethod = "POST"

答案 1 :(得分:0)

我通过在HTTPMethod-> func requestServer(_ method: Alamofire.HTTPMethod,_ path: String,_ params: [String: Any]?,_ encoding: ParameterEncoding,_ completionHandler: @escaping (JSON) -> Void)

之前添加Alamofire摆脱了错误

但是现在我下面的代码有一个问题,说明以下内容:'nil'与预期的参数类型'JSON'不兼容

'AF.request(url !, method:方法,参数:params,编码:编码,标头:nil).responseJSON {response in

            switch response.result {
            case .success(let value):
                let jsonData = JSON(value)
                completionHandler(jsonData)
                break


            case .failure:
                completionHandler(nil)     <------ this is a trigger
                break

            }
        }
    }

}

我通过应用nil解决了这个问题!这使我可以绕过错误,但是在应用程序内部,我无法注销并收到以下错误:

2019-09-04 08:13:22.518630-0400 DeliveryMobile [980:52117] [] nw_socket_handle_socket_event [C12.2:2]套接字SO_ERROR [61:连接被拒绝] 2019-09-04 08:13:22.530669-0400 DeliveryMobile [980:52117] TIC读取状态[12:0x60000031eac0]:1:57 2019-09-04 08:13:25.302686-0400 DeliveryMobile [980:52131] [] nw_socket_handle_socket_event [C13.1:2]套接字SO_ERROR [61:连接被拒绝] 2019-09-04 08:13:25.305162-0400 DeliveryMobile [980:52131] [] nw_socket_handle_socket_event [C13.2:2]套接字SO_ERROR [61:连接被拒绝] 2019-09-04 08:13:25.325531-0400 DeliveryMobile [980:52992] TI​​C读取状态[13:0x60000031eac0]:1:57 2019-09-04 08:13:34.886240-0400 DeliveryMobile [980:52558] [] nw_socket_handle_socket_event [C14.1:2]套接字SO_ERROR