Why the use of ampersand in parsing JSON data when specifiying error in Swift?

202 views Asked by At

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!

1

There are 1 answers

1
gnasher729 On BEST ANSWER

& 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.