Book Image

JavaScript JSON Cookbook

By : Ray Rischpater, Brian Ritchie, Ray Rischpater
Book Image

JavaScript JSON Cookbook

By: Ray Rischpater, Brian Ritchie, Ray Rischpater

Overview of this book

Table of Contents (17 chapters)
JavaScript JSON Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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.