26

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>