2016-09-28 91 views
0

我在嘗試從Google博客解析某些JSON時出錯。在Swift中解析json時出錯

我收到的錯誤是

'Invalid conversion from throwing function of type '(_, _, _) throws -> Void' to non-throwing function type '(NSData?, NSURLResponse?, NSError?) -> Void' 

我身邊有一派,我知道這是什麼做追趕的錯誤,但我想不出它到底是什麼,任何幫助將不勝感激:

import UIKit 
import CoreData 

class MasterViewController: UITableViewController { 

var detailViewController: DetailViewController? = nil 
override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    let url = NSURL(string: "https://www.googleapis.com/blogger/v3/blogs/10861780/posts?key=AIzaSyBwmI4AzMnBmr7oSVeL0EHdzMjXV1aATnQ") 

    let session = NSURLSession.sharedSession() 

    let task = session.dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in 
    if error != nil { 
      print(error) 
     } else { 
      //print(NSString(data: data!, encoding: NSUTF8StringEncoding)) 

      do { 

      let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary 
      if jsonResult.count > 0 { 

       if let items = jsonResult["items"] as? NSArray { 
        for items in items { 
         print(items) 

        } 
+0

添加「抓讓誤差NSError { 打印(error.localizedDescription) }」 – Ragul

+0

如果有這樣的在右括號後的底部剛過打印(項目)增加?如果不是我應該添加這個嗎?謝謝! –

+0

add do {} – Ragul

回答

0

試試這個。我糾正了你的錯誤,只加了一些括號。

override func viewDidLoad() { 
    super.viewDidLoad() 

let url = NSURL(string: "https://www.googleapis.com/blogger/v3/blogs/10861780/posts?key=AIzaSyBwmI4AzMnBmr7oSVeL0EHdzMjXV1aATnQ") 

let session = NSURLSession.sharedSession() 

let task = session.dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in 

    if error != nil { 
    print(error) 
    } else { 
    //print(NSString(data: data!, encoding: NSUTF8StringEncoding)) 
    do { 

     let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions(rawValue: 0)) as! NSDictionary 
     if jsonResult.count > 0 { 

     if let items = jsonResult["items"] as? NSArray { 
      for items in items { 
      print(items) 

      } 
     } 
     } 
    } catch let error as NSError { 
     print(error) 
    } 
    } 
}) 
    task.resume() 

} // end braces of view did load 
+0

這已編譯好,似乎並沒有顯示任何錯誤謝謝,除了預期的輸出不顯示:S –

+0

現在嘗試。現在應該工作正常 – Sahil

+0

害怕說它仍然沒有打印博客通知( –

0

試試這個,

do { 
     let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary 
     if jsonResult.count > 0 { 

      if let items = jsonResult["items"] as? NSArray { 
       for items in items { 
        print(items) 

       } 
      } 
    }catch{ 
      print("Something wrong") 
     } 

並替換該行

let task = session.dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in 

let task = session.dataTaskWithRequest(request) { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in 
+0

嗨,我試了這個,並收到相同的兩個錯誤,'無效轉換等投擲等。'和'連續的語句在一條線上必須分開';' –

+0

回答更新嘗試上述 – Ragul

+0

如果它不起作用,請在代碼窗口上傳錯誤代碼的屏幕截圖 – Ragul