Safely Working with Untyped Data Structures

Congratulations, it works! But are you finished? Your application already detects errors from JSON parsing (converting the received JSON data into a tree of NSDictionary, NSArray, and so forth), but it does not check that the structure of this data satisfies the expectations of the app’s code.

As it stands, the code makes some assumptions about the structure of the JSON. Consider these lines:

 ​ ​ ​ ​ ​ ​ ​ ​l​e​t​ ​t​i​t​l​e​ ​=​ ​c​o​u​r​s​e​D​i​c​t​[​"​t​i​t​l​e​"​]​ ​a​s​!​ ​S​t​r​i​n​g​ ​ ​ ​ ​ ​ ​ ​ ​l​e​t​ ​u​r​l​S​t​r​i​n​g​ ​=​ ​c​o​u​r​s​e​D​i​c​t​[​"​u​r​l​"​]​ ​a​s​!​ ​S​t​r​i​n​g​ ​ ​ ​ ​ ​ ​ ​ ​l​e​t​ ​u​p​c​o​m​i​n​g​A​r​r​a​y​ ​=​ ​c​o​u​r​s​e​D​i​c​t​[​"​u​p​c​o​m​i​n​g​"​]​ ...

Get Cocoa Programming for OS X: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.