0

When parsing data with NSJSONSerialization, my code will not compile unless I specify the parsing error with an ampersand before it, as such:

var parsingError: NSError? = nil
            let parsedResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: &parsingError) as! NSDictionary

You can see it in

error: &parsingError

Could someone explain why this is and what the meaning of the ampersand is here?

Thanks a lot!

Julius
  • 1,451
  • 1
  • 15
  • 19

1 Answers1

1

& is required for in-out parameters.

I hope you are aware that your app will crash if it doesn't receive a JSON dictionary. Which is very likely to happen if you are behind a paywall, for example. So your app is likely to crash if I try it in a hotel with free Wi-Fi. Passing AllowFragments seems rather pointless, because JSON fragments are not dictionaries.

gnasher729
  • 51,477
  • 5
  • 75
  • 98