Book Image

Swift Essentials - Second Edition

By : Alex Blewitt
Book Image

Swift Essentials - Second Edition

By: Alex Blewitt

Overview of this book

Swift was considered one of the biggest innovations last year, and certainly with Swift 2 announced at WWDC in 2015, this segment of the developer space will continue to be hot and dominating. This is a fast-paced guide to provide an overview of Swift programming and then walks you through in detail how to write iOS applications. Progress through chapters on custom views, networking, parsing and build a complete application as a Git repository, all by using Swift as the core language
Table of Contents (17 chapters)
Swift Essentials Second Edition
Credits
About the Author
Acknowledgments
About the Reviewer
www.PacktPub.com
Preface
Index

Parsing JSON


The most popular mechanism to send structured data over a network is to encode it in JSON, which stands for JavaScript Object Notation. This provides a hierarchical tree data structure, which can store simple numeric, logical, and string-based types, along with array and dictionary representations.

Both Mac OS X and iOS come with a built-in parser for JSON documents, in the NSJSONSerialization class. This provides a means to parse a data object and return an NSDictionary that contains the key/value pairs of a JSON object, or an NSArray to represent JSON arrays. Other literals are parsed and are represented as either NSNumber or NSString values.

The JSON parser uses JSONObjectWithData to create an object from an NSData object containing a string. This is typically the format that is returned by network APIs, and it can be created from an existing string using dataUsingEncoding with one of the built-in encoding types, such as NSUTF8StringEncoding.

A simple JSON array of numbers can...