2015-10-06 48 views
-1

我想爲從Google地圖接收的JSON做出錯誤處理, 事情是我已經在Swift 1.2中寫過它,但是當我遷移到Swift 2.0和XCode 7 Beta時,問題上升, 這裏是代碼雨燕1.2:錯誤處理Swift 2.0谷歌地圖Json

var error: NSError? 

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

回答

1

在SWIFT 2.0,就需要抓...

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

可能會有一些變化需要......但是這是你的方式必須在迅速2.0