2015-06-26 47 views
46

我目前正在使用Swift 2.0和Xcode Beta 2開發我的第一個iOS應用程序。它讀取一個外部JSON,並在數據的表視圖中生成一個列表。但是,我得到一個奇怪的小錯誤,我似乎無法修復:Swift:調用中的額外參數「錯誤」

Extra argument 'error' in call 

這裏是我的代碼片段:

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

      if(error != nil){ 
       print(error!.localizedDescription) 
      } 

      var err: NSError? 

      if let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as? NSDictionary{ 

       if(err != nil){ 
        print("JSON Error \(err!.localizedDescription)") 
       } 

       if let results: NSArray = jsonResult["results"] as? NSArray{ 
        dispatch_async(dispatch_get_main_queue(), { 
         self.tableData = results 
         self.appsTableView!.reloadData() 
        }) 
       } 
      } 
     }) 

錯誤是在這行拋出:

if let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as? NSDictionary{ 

有人可以告訴我我在做什麼錯嗎?

+0

這條線沒有給出這個錯誤,我想可能是你在不同的路線上。 –

+0

在2.0中,你需要創建一個'do'' catch'塊。 'error'不再是'NSJSONSerialization'的參數。這裏有很多其他的答案。找工作,趕上Swift 2.0 –

回答

75

隨着斯威夫特2,則簽名NSJSONSerialization已經改變,以符合新的錯誤處理系統。

這裏有一個如何使用它的一個例子:

do { 
    if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? NSDictionary { 
     print(jsonResult) 
    } 
} catch let error as NSError { 
    print(error.localizedDescription) 
} 

隨着斯威夫特3,則NSJSONSerialization及其方法發生了變化,根據the Swift API Design Guidelines

這是同樣的例子:

do { 
    if let jsonResult = try JSONSerialization.jsonObject(with: data, options: []) as? [String:AnyObject] { 
     print(jsonResult) 
    } 
} catch let error as NSError { 
    print(error.localizedDescription) 
} 
+0

我使用的是swift 2.2,並試過了你的第一個版本。它正在編譯,但它正在悄悄地死去,並沒有碰到任何'print'行。當我移除'jsonResult'前面的'if'部分並運行時,我可以推斷出jsonResult被設置爲'nil'沒有錯誤。我確信數據以一個有效的json字符串開頭...... –

+1

@jeffery_the_wind您的JSON可能與您的想法不同。試試這個:http://stackoverflow.com/a/33510776/2227743 – Moritz

+1

謝謝,是的,就是這樣!代替'as? NSDictionary'與'as?你的第一個例子中的NSArray'適合我。 –

5

事情已經在斯威夫特2改變,接受了一個error參數轉化成拋出錯誤,而不是通過inout參數返回它的方式方法。通過查看Apple documentation

處理錯誤斯威夫特: 在斯威夫特,這個方法返回一個非可選結果,並標有拋出關鍵字,以表明它在失敗的情況下,拋出一個錯誤。

您可以在try語句中調用此方法,並處理do語句的catch子句中的任何錯誤,如Swift編程語言(Swift 2.1)中的錯誤處理和使用Swift與Cocoa和Objective- C(Swift 2.1)。

最短的解決方案是使用try?返回nil如果發生錯誤:

let message = try? NSJSONSerialization.JSONObjectWithData(receivedData, options:.AllowFragments) 
if let dict = message as? NSDictionary { 
    // ... process the data 
} 

如果你也有興趣到錯誤,你可以使用一個do/catch

do { 
    let message = try NSJSONSerialization.JSONObjectWithData(receivedData, options:.AllowFragments) 
    if let dict = message as? NSDictionary { 
     // ... process the data 
    } 
} catch let error as NSError { 
    print("An error occurred: \(error)") 
} 
+0

這在Swift 2.2上適用於我 – swiftBoy

0

這已在Swift 3.0中進行了更改。

do{ 
      if let responseObj = try JSONSerialization.jsonObject(with: results, options: .allowFragments) as? NSDictionary{ 

       if JSONSerialization.isValidJSONObject(responseObj){ 
        //Do your stuff here 
       } 
       else{ 
        //Handle error 
       } 
      } 
      else{ 
       //Do your stuff here 
      } 
     } 
     catch let error as NSError { 
       print("An error occurred: \(error)") }