您好,我有适用于swift2.2的json解析代码,但是当我将其用于Swift 3.0时,出现了该错误
ViewController.swift:132:31:成员’dataTask(with:completionHandler :)’的引用不明确
我的代码在这里
let listUrlString = "http://bla.com?batchSize=" + String(batchSize) + "&fromIndex=" + String(fromIndex) let myUrl = URL(string: listUrlString); let request = NSMutableURLRequest(url:myUrl!); request.httpMethod = "GET"; let task = URLSession.shared().dataTask(with: request) { data, response, error in if error != nil { print(error!.localizedDescription) DispatchQueue.main.sync(execute: { AWLoader.hide() }) return } do { let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSArray if let parseJSON = json { var items = self.categoryList items.append(contentsOf: parseJSON as! [String]) if self.fromIndex < items.count { self.categoryList = items self.fromIndex = items.count DispatchQueue.main.async(execute: { self.categoriesTableView.reloadData() AWLoader.hide() }) }else if( self.fromIndex == items.count){ DispatchQueue.main.async(execute: { AWLoader.hide() }) } } } catch { AWLoader.hide() print(error) } } task.resume()
感谢您的想法。
函数签名使编译器感到困惑。您可以这样解决:
let task = URLSession.shared.dataTask(with: request as URLRequest) {
但是请注意, 如果 早先声明为而不是,则我们不必像URLRequest此签名一样强制转换“ request” : __URLRequest``NSMutableURLRequest
URLRequest
URLRequest``NSMutableURLRequest
var request = URLRequest(url:myUrl!)
这是NSMutableURLRequest新的之间的自动转换,并且自动转换URLRequest失败,这迫使我们在这里进行转换。
NSMutableURLRequest