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.