错误处理Swift 2.0谷歌地图Json



我想对从google地图接收的JSON进行错误处理,问题是我已经用Swift 1.2写过了,但当我迁移到Swift 2.0和XCode 7 Beta时,问题出现了,下面是Swift 1.2的代码:

  var error: NSError?
          let dictionary: Dictionary<NSObject, AnyObject> = NSJSONSerialization.JSONObjectWithData(geocodingResultsData!, options: NSJSONReadingOptions.MutableContainers, error: &localError) as! Dictionary<NSObject, AnyObject>

在swift 2.0中,需要do catch…

do {
    let dictionary = try NSJSONSerialization.JSONObjectWithData(geocodingResultsData, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary
} catch let error as NSError {
     print(error)
}

可能需要一些改变…但是在swift 2.0中你必须这样做

最新更新