Return Value

JSON data for obj, or nil if an internal error occurs. The resulting data is encoded in UTF-8.

Discussion

If obj will not produce valid JSON, an exception is thrown. This exception is thrown prior to parsing and represents a programming error, not an internal error. You should check whether the input will produce valid JSON before calling this method by using isValidJSONObject:.

Setting the NSJSONWritingPrettyPrinted option will generate JSON with whitespace designed to make the output more readable. If that option is not set, the most compact possible JSON will be generated.

Handling Errors in Swift:

In Swift, this method returns a nonoptional result and is marked with the throws keyword to indicate that it throws an error in cases of failure.