Any way to combine try with as?

I had some 1.2 code that got converted to 2.0 today. However, it appears I need to add more indention to handle it. Not sure if there is a better way:


do {
  let orders = try NSJSONSerialization.JSONObjectWithData(d, options: NSJSONReadingOptions())
  if let orders = orders as? [[String: AnyObject]] {
   ...


I use to have a single if let ... statement here where I combined asking for the jsonObject with "as? [[String: AnyObject]]"


Is there a better way to do this or is what I have here the right way to do it?

Answered by dhoerl in 8215022

OK - using more edits figured it out - in case anyone else interested:


  do {
  if let x = try NSJSONSerialization.JSONObjectWithData(op.webData, options: NSJSONReadingOptions()) as? [String: AnyObject] {
Accepted Answer

OK - using more edits figured it out - in case anyone else interested:


  do {
  if let x = try NSJSONSerialization.JSONObjectWithData(op.webData, options: NSJSONReadingOptions()) as? [String: AnyObject] {

Can you post the rest of the statement? I'd to see how the catch interacts with the if let.

...Its just as you expect given the syntax I used:


do {
  if let x = try  NSJSONSerialization ... as? [[ ..., ... ]]{
   blah blah blah
  } // closing brace of let
} catch {
  print(error)
}


This was just not obvious to me and I had to play around with it before it compiled, I still don't know if it works since I am still working on fixing 2.0 conversion problems...


I entered a documentation bug report on this, since the try / catch section of the Swift programming guide doesn't touch on any of this (rdar://21330154)

Any way to combine try with as?
 
 
Q