iOS Networking with Swift: Lesson 3: Problem Set: JSON Parsing

These are good exercises, worth doing, with the following caveats.

For the Animals Exercise, I found it completely unnecessary to write any code. I just looked at the raw JSON file and answered the questions using that. Thankfully, the next exercise data set was both large enough and complicated enough that writing code appears to be the quicker solution.

For the Hearthstone Exercise, I could not successfully get the hearthstone.playground (or the hearthstone-solution.playground for that matter) to execute more than once. Any changes to the code would send Xcode off into NeverNeverLand until I completely quit Xcode and restarted the playground. (This is Xcode version 8.3.2, by the way.)

My solution to the problem was two-fold, but perhaps only one of these is necessary:

  1. Set the playground to “Manually Run”. By default, the playground will automatically run. To change it to only run when you want it to run, long-click the square (if the playground is currently running) or the right-facing “play” triangle (if the playground is paused or “Ready”) at the bottom-left of the Editor area and select “Manually Run”. Now, when you want the playground to run, click the right-facing “play” triangle.
  2. Rebuild the playground from scratch. In particular, I removed all forced unwrapping (‘!’) and references to NSDictionary. Furthermore, there is no need to pass the parsedHearthstoneJSON variable into the parseJSONAsDictionary() function. (You will note that the argument dictionary is never referenced anywhere inside the function.) Although putting the parsing into a function is a good idea for app code, it is unnecessary here in this playground.

Leave a Reply

Your email address will not be published. Required fields are marked *