Swift 2.0 added do {} catch {} which can be used like so:
do { let jsonData = try NSJSONSerialization.dataWithJSONObject(params, options: []); } catch let jsonError as NSError { print(jsonError); } but I've also seen in in my 2.0 converted classes the catch implemented with an underscore:
do { let jsonData = try NSJSONSerialization.dataWithJSONObject(params, options: []); } catch _ { } what makes it especially confusing is why not just provide nothing after catch which is perfectly valid code:
do { let jsonData = try NSJSONSerialization.dataWithJSONObject(params, options: []); } catch { } What is that underscore _, what can I do with it, how would I implement that in my own function signatures? In the previous Swift 1.2 declaration I didn't use the NSError so it makes sense that the conversion is throwing the error away but why use _?
catch {so why would it convert to having a_?jsonErrorwasn't actually used in the catch block Xcode may automatically conver this to_. If it's because of this maybe they just didn't implement the process cleverly enough to do it without an_if one isn't required.if let ...if the variable name was not actually used in the following block. If they were to not put the_in these places the code wouldn't actually work. Hence it does_everywhere to be safe, even though in some places you may not actually need it.