Reputation: 13
I am using swiftyjson for JSON Parsing.
My problem is when url is not valid or url is returns any error then app crash
here is code
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { () -> Void in
let url = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=25&playlistId=\(self.playId)&key=AIzaSyB6LBckj5oqkbmroNPIhm7zru9vmsZR-w0"
self.json = JSON(data: (NSData(contentsOfURL: (NSURL(string: url)!)))!, options: NSJSONReadingOptions.MutableContainers, error: nil) // App is crash on this line
// END
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableView.reloadData()
})
How to check url is valid or url is contain error
Upvotes: 0
Views: 517
Reputation: 2407
You should use try-catch something like this to prevent the crash:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { () -> Void in
let url = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=25&playlistId=\(self.playId)&key=AIzaSyB6LBckj5oqkbmroNPIhm7zru9vmsZR-w0"
if let JSONObject = try NSJSONSerialization.JSONObjectWithData((NSData(contentsOfURL: (NSURL(string: url)!)))?, options: .AllowFragments) as? [[String: AnyObject]],
}
// END
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableView.reloadData()
})
Upvotes: 1