gpt4 book ai didi

ios - Swift 3 - 无法调用非函数类型的值 '(response: Any, error Any)'

转载 作者:行者123 更新时间:2023-11-28 16:08:21 25 4
gpt4 key购买 nike

我在 Swift 3 中写了下面的方法

func postFormData(url : String, parameters: Parameters!, headers : HTTPHeaders!, completionHandler : (response : Any,  error: Any)) {

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

if let status = response.response?.statusCode {
switch(status)
{
let data = result as! NSDictionary
case 200:
if let result = response.result.value {
completionHandler(data)
}
default:
completionHandler(nil, data)
}
}
}

}
  1. 我在 completionHandler(data) 处收到一个错误,指出 Cannot call value of non-function type '(response: Any, error Any)'
  2. 编写 completionHandler(nil, data) 是处理错误的最佳实践吗?

PS:我是 iOSSwift 3

的新手

最佳答案

您缺少 completionHandler -> Void 和参数名称的返回值,我在此处添加了默认值 _:

func postFormData(url : String, parameters: Parameters!, headers : HTTPHeaders!, completionHandler : (_ response : Any,  _ error: Any) -> Void ) {

Alamofire.request(url, method : .post, parameters: parameters, headers: headers)
.responseJSON { response in
if let status = response.response?.statusCode {
switch(status)
{
let data = result as! NSDictionary
case 200:
if let result = response.result.value {
completionHandler(data)
}
default:
completionHandler(nil, data)
}
}
}

}

对于错误部分,我会添加一个 onError 作为 , onError: (NSError) -> Void) 并在发生错误时返回 onError。所以像这样:

func postFormData(url : String, parameters: Parameters!, headers : HTTPHeaders!, completionHandler : (_ response : Any,  _ error: Any) -> Void, onError: (NSError) -> Void)

关于ios - Swift 3 - 无法调用非函数类型的值 '(response: Any, error Any)',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39904075/

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