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
}

SwifyJSON 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.

FacebookTwitterDiggStumbleUponGoogle Plus

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

©2014 iOS App Dev Libraries, Controls, Tutorials, Examples and Tools. All Rights Reserved.

Leave a Reply

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