O'Reilly logo

JavaScript JSON Cookbook by Ray Rischpater

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Parsing JSON on iOS using Swift

The same NSJSONSerialization class is available in Swift, Apple's new language for iOS development.

How to do it…

Here's an example of how to invoke the JSONObjectWithData method of NSJSONSerialization in Swift:

import Foundation
var error: NSError?
Let json: NSData = /* the JSON to parse */
let data = NSJSONSerialization.JSONObjectWithData(json, 
  options: nil, 
  error: &error);

How it works…

Method invocations in Swift look like function invocations, with the arguments passed as (optionally named) comma-delimited arguments, similar to how they're invoked in C++ or Java. The arguments to JSONObjectWithData are identical to the method arguments in the Objective-C version.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required