Categories
Featured iPhone Development Resources iOS Development Libraries Open Source iOS Libraries And Tools Swift

An Open Source Library That Makes Working With JSON Data Using Swift Much Easier

Handling JSON data can be difficult using Swift because of Swift’s strictness with types.

Here’s a library that makes handling Swift in much easier called SwiftyJSON from Ruoyu Fu.

SwiftyJSON makes things much simpler automatically handling different types of data.

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 also handles wrapping of values, and handling incorrect keys.

You can find SwiftyJSON on Github here.

A nice library for working with JSON with Swift.