我目前正在使用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{
有人可以告訴我我在做什麼錯嗎?
這條線沒有給出這個錯誤,我想可能是你在不同的路線上。 –
在2.0中,你需要創建一個'do'' catch'塊。 'error'不再是'NSJSONSerialization'的參數。這裏有很多其他的答案。找工作,趕上Swift 2.0 –