Swift is very strict about types, It’s good while explicit typing left us little chance to make mistakes. But while dealing with things that naturally implicit about types such as JSON, It’s painful.

SwiftyJSON makes it easy to deal with JSON data in Swift.

This example from the readme shows the mess caused from using NSJSON to handle parsing the user and name from JSON data even when using chaining:

let jsonObject : AnyObject! = NSJSONSerialization.JSONObjectWithData(dataFromTwitter, options: NSJSONReadingOptions.MutableContainers, error: nil)
if let userName = (((jsonObject as? NSArray)?[0] as? NSDictionary)?["user"] as? NSDictionary)?["name"]{
  //What A disaster above

While with SwiftyJSON you could use something like:

let json = JSONValue(dataFromNetworking)
if let userName = json[0]["user"]["name"].string{
  //Now you got your value

SwiftyJSON on GitHub

  1. bogus email address because I don't want to give it to you says:

    Library doesn’t work. Keep getting “doesn’t conform to protocol …” <- multiple errors…

    • Darrarski says:

      Hey, I will check it out when I get back from vacation. In the meantime consider opening an issue on project’s GitHub page. Swift is still in beta phase, it changes from version to version, that’s possible that something gets broken in the latest beta.

Comments are closed.