13.07.2015 Views

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

176CHAPTER 6: Networking: Connections, Data, and the CloudNetwork DataData sent across the network can be anything, but the most common formats currentlyused are JSON (JavaScript Object Notation) and XML (eXtensible Markup Language). TheFoundation framework on OS X and iOS provides facilities for the handling of both typesof data.Reading and Writing JSONThe Foundation framework provides the NSJSONSerialization class for working withJSON. This class converts between JSON string data and property list types such asdictionaries, arrays, strings, and numbers. Figure 6-2 shows how the data is mapped todifferent types.{names : [Tim,Steve,Phil];NSDictionary@“names” :NSArray@“Tim”@“Steve”@“Phil”}address : {street: “1 Infinite Loop”;city: Cupertino;state: California;zip: 95014;}@“address” :NSDictionary@“street” : @“1 Infinite Loop”@“city” : @“Cupertino”@“state” : @“California”@“zip” : @95014Figure 6-2. An example of equivalent JSON and property list typesWhen reading JSON data, all keyed lists (elements between { and } characters) are turned intoNSDictionary instances, and all flat arrays (elements between [ and ] characters) are turnedinto NSArray instances. Any strings encountered along with the names of named items in keyedlists are turned into NSStrings, and all purely numeric strings (except for keys) are representedas NSNumbers. Lastly, any values of null are represented using NSNull.The same thing applies if you’re going the other way, of course: any array or dictionarycan be transformed into JSON data, provided it only contains the objects specifiedabove. The jsonize sample project (available in its entirety online) converts betweenproperty list and JSON formats. Its core conversion method is shown in Listing 6-7.www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!