2016-11-22 111 views
1

你读到这篇文章之前,请理解,我有点斯威夫特与一个初学者,我努力学习。我已经通过一些网站在得到答案的希望浏览,但我要么无法找到或做错误的。我一直在关注这个教程,但这是老不更新>>Tutorial I followed < <URLSession&JSONSerialization斯威夫特3

我也试图修改一些快速3 - 虽然我可能没有这样做。

我该如何正确地做URLSession?我得到这个错误:

invalid conversion from throwing function of type'(_, _, _) throws -> Void' to non-throwing function type

下面这一行:

let task : URLSessionDataTask = session.dataTask(with: request, completionHandler: {data, response, error -> Void in

和变量 “jsonDict” - 我得到的错误,我有事先

extra arg 'error' in call.

谢谢

var urlString:String = ("http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.quotes where symbol IN "+stringQuotes+"&format=json&env=http://datatables.org/alltables.env").addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)! 

    var url : URL = URL(string: urlString)! 
    var request: URLRequest = URLRequest(url:url) 
    let config = URLSessionConfiguration.default 
    let session = URLSession(configuration: config) 

    let task : URLSessionDataTask = session.dataTask(with: request, completionHandler: {data, response, error -> Void in 

     if((error) != nil) { 
      println(error.localizedDescription) 
     } 
     else { 
      var err: NSError? 

      var jsonDict = try JSONSerialization.JSONObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers, error: &err) as NSDictionary 
      if(err != nil) { 
       println("JSON Error \(err!.localizedDescription)") 
      } 
      else { 
       var quotes:NSArray = ((jsonDict.objectForKey("query") as NSDictionary).objectForKey("results") as NSDictionary).objectForKey("quote") as NSArray 
       DispatchQueue.main.async(execute: { 
        .default.post(name: Notification.Name(rawValue: kNotificationStocksUpdated), object: nil, userInfo: [kNotificationStocksUpdated:quotes]) 
       }) 
      } 
     } 
    }) 
    task.resume() 
} 
+0

它应该是'var jsonDict = try JSONSerialization.JSONObject(with:data,options:JSONSerialization.ReadingOptions.mutableContainers)as NSDictionary',in swift 3 error argument removed – Vinodh

+0

@Vinodh - 谢谢,我不认为这会很简单。 – zya

+0

邮政它作为一个答案它会为别人 – Vinodh

回答

1

请找到更新的代码为雨燕3.0

var urlString:String = ("http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.quotes where symbol IN "+stringQuotes+"&format=json&env=http://datatables.org/alltables.env").addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)! 

     let url : URL = URL(string: urlString)! 
     let request: URLRequest = URLRequest(url:url) 
     let config = URLSessionConfiguration.default 
     let session = URLSession(configuration: config) 

     let task = session.dataTask(with: request) { (data, response, error) in 

      if(error != nil){ 
       print(error?.localizedDescription ?? "") 
      } 
      else{ 
       do{ 
        let jsonDict:NSDictionary = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! NSDictionary 
        let quotes:NSArray = ((jsonDict.object(forKey: "query") as! NSDictionary).object(forKey: "results") as! NSDictionary).object(forKey: "quote") as! NSArray 
        print(quotes) 

       } 
       catch{ 
        print(error.localizedDescription) 
       } 
      } 
     }; 
     task.resume() 

注:我没有测试的代码。因为你没有指定URL参数

+0

似乎是固定它,我想。谢谢!当你的意思是URL的参数...这样? '/http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.quotes where symbol IN(“AAPL”,“GOOG”,“FB”)&format = json&env = http: // datatables.org/alltables.env' – zya

+0

是的,你是正确的我不知道你在传递什么'stringQuotes',现在你在评论中提到。谢谢 – Vinodh

+0

一个更改我添加的url编码方法应该是'urlFragmentAllowed'而不是'urlHostAllowed',如果我使用'urlHostAllowed'无法达到雅虎api – Vinodh

0

你的问题是不是在URLSessionDataTask但在您的completionHandler。更具体地说在var jsonDict = try JSONSerialization...位。 try表示您的代码可能会抛出异常,但您不处理它(catch)。这就是为什么编译器决定你的完成处理程序类型为(_, _, _) throws -> Void,而dataTask方法需要(_, _, _) -> Void

Here你可以找到信息如何使用try/catch

+0

我想,但我一直在做的错误,这是为什么我在这里寻求帮助。 为URLSession原单的代码是这样**让任务:NSURLSessionDataTask = session.dataTaskWithRequest(请求,completionHandler:{数据,响应错误 - >虚空中** – zya

+0

这解决了这个问题对我来说 ' 做{VAR josnDict = try JSONSer ... ... }) } catch { //句柄错误 } ' – Valdmer