gpt4 book ai didi

swift - Swift3 : cannot understand new syntax for completion handlers 的许多问题

转载 作者:可可西里 更新时间:2023-11-01 00:21:34 26 4
gpt4 key购买 nike

昨天我更新到新的 Mac OS X Sierra 和 XCode 8,这迫使我更新到 Swift 3.0 语法。在我的应用程序中,我有很多功能,如下所示:

fileprivate func requestFisheFieldWithHandler(_ url:String, completionHandler: @escaping (_ success: NSDictionary?, _ error: NSError?) -> Void) {

let configuration = URLSessionConfiguration.default

let url: URL = URL(string: url)!
let urlRequest: URLRequest = URLRequest(url: url)

let session = URLSession(configuration: configuration)

let task = session.dataTask(with: urlRequest, completionHandler: { (data: Foundation.Data?, response: URLResponse?, error: NSError?) -> Void in

if (error != nil) {
//print(error?.code)
//print(error)
completionHandler(success: nil, error: error)
}

else {
do {
let responseJSON = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions()) as! [String: String]
completionHandler(success: responseJSON, error:nil)
}
catch let error as NSError {
completionHandler(success: nil, error:error)
}
}
} as! (Data?, URLResponse?, Error?) -> Void)

task.resume()
}

我得到这个错误:

"Cannot convert value of type '(Data?, URLResponse?, Error?) -> Void' to expected argument type '(Data?, URLResponse?, Error?) -> Void'"

此外,我还使用了许多关联数组从下载的 JSON 文件中收集数据,如下所示:

for comune in response! {
self.comuni.append(comune["nome"] as! String)
self.comuniWithID[comune["nome"] as! String] = Int(comune["idcomune"] as! String)
}
DispatchQueue.main.async {
self.myPicker.reloadComponent(1)
}

我得到的另一个错误是:

"Type 'NSFastEnumerationIterator.Element' (aka 'Any') has no subscript members"

拜托,有人能帮我改正吗?因为我不明白他们的意思,我的应用程序将于明年 9 月 30 日发布...

最佳答案

最显着的变化是在 Swift 3 中移除了闭包中的所有参数标签。

这是与 Swift 3 兼容的代码。

一如既往,不要将 Swift 集合类型转换为 Foundation 对应物。您将丢弃所有类型信息。

并且不要在完成 block 返回值中使用注释,编译器可以推断类型。如果您需要查找实际类型,请⌥-单击符号。

fileprivate func requestFisheFieldWithHandler(_ url:String, completionHandler: @escaping ([String: String]?, NSError?) -> Void) {

let configuration = URLSessionConfiguration.default

let url: URL = URL(string: url)!
let urlRequest = URLRequest(url: url)

let session = URLSession(configuration: configuration)

let task = session.dataTask(with: urlRequest) { (data, response, error) -> Void in
if (error != nil) {
completionHandler(nil, error as NSError?)
}

else {
do {
let responseJSON = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions()) as! [String: String]
completionHandler(responseJSON, nil)
}
catch let error as NSError {
completionHandler(nil, error)
}
}
}
task.resume()
}

关于第二个错误,您必须将 response! 转换为比 Any 更有意义的内容,我猜 ... 作为响应! [[String:Any]]

关于swift - Swift3 : cannot understand new syntax for completion handlers 的许多问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39676513/

26 4 0