-1

I'm new to Swift and I am trying to generate JSON data from an NSDictionary

Here's my code:

func metricsToJson() {
        if let data = NSJSONSerialization.dataWithJSONObject(metricsData, options: NSJSONWritingOptions.PrettyPrinted, error: &error) {
            if let json = NSString(data: data, encoding: NSUTF8StringEncoding) {
                println(json)
            }

        }
    }

enter image description here I'm not sure why this isn't working, but any guidance would be greatly appreciated.

Varun Varahabotla
  • 548
  • 2
  • 7
  • 16
  • Some mighty impressive code you've got there. (Paste the code into your question, highlight it, then press the `{}` button above the editing window. Don't embed images of code, even if you manage to do it correctly.) – Hot Licks Jul 27 '15 at 18:54
  • Sorry about that. I wanted to show the screenshot so you could see the error – Varun Varahabotla Jul 27 '15 at 18:57

1 Answers1

0

Try below code, it may help to solve your problem.

var jsonResult = NSJSONSerialization.JSONObjectWithData(metricsData, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSArray

for var i = 0; i<jsonResult.count; ++i {

    var dictResult = jsonResult.objectAtIndex(i) as! NSDictionary
    // your code...
}  
VRAwesome
  • 4,721
  • 5
  • 27
  • 52