Let's assume you have a dictionary like
let dictionary = ["key1": "value1", "key2": "value2"]
To generate your JSON Data using the method you provided you just call it:
var jsonGenerationError: NSError?
let jsonData = NSJSONSerialization.dataWithJSONObject(dictionary, options: .PrettyPrinted, error: &jsonGenerationError)
You can verify it worked by parsing the generated data:
var jsonParsingError: NSError?
let parsedObject: AnyObject? = NSJSONSerialization.JSONObjectWithData(jsonData!, options: .AllowFragments, error:&jsonParsingError)
Swift 4
let dictionary = ["key1": "value1", "key2": "value2"]
let jsonData = try? JSONSerialization.data(withJSONObject: dictionary, options: .prettyPrinted)
// Verifying it worked:
let parsedObject = try! JSONSerialization.jsonObject(with: jsonData!, options: .allowFragments)