Swift JSON Parser – Part 2

In this post we continue the development of a JSON Parser starting from the previous one: Swift JSON Parser – Part 1.

Let us begin handling the “something went wrong” part.

Error handling strategy

Two types of “bad things” can happen:

  1. The parser has an error before looping on the elements of the array: this can be a reader error or the json returned is not of type [AnyObject?]
  2. The parser starts but has an error on one of the element inside the loop

For errors of type 1 we add a second callback parameter to start.

Add a return value to the handleData method:

and modify start:

The method calling the parser will handle a possible error using the second parameter of start (a callback):

Now we have to handle the errors of type 2, the ones arising inside the loop. For this we modify the callback ParserNewPhoto from:

to:

What is PhotoResult?

We are use the same technic we used for the reader: an enum with associated value:

The enum evals to different values depending on the result of the operation.

If a photo was created successfully we invoke the callback with:

otherwise:

Why not a “generic”?

We defined twovery similar enums:

This situation would be perfect for a swift generic:

And THIS IS THE RIGHT IMPLEMENTATION. But at the moment (and since some time) there is a bug in the swift compiler that prevent this:

enum_bug

There are some workarounds, like boxing the value inside a class and make that class the associated value, like in:

but I prefer not to confuse the (already complex) situation. Remember to fix this when Apple fix its bug!

Continue or stop?

The parser wants to know what to do after an error. Should it continue or stop execution? This is why we added the Bool return value to the ParserNewPhoto callback: we ask it to the callback. By the way I don’t like very much this name “ParserNewPhoto”, when we will make this parser generic (not about photos) we will change that name.

This is the code inside loop with error handling:

And the parser caller (in viewDidLoad of our project):

Ok for now this is enough for error handling.

More elegant optional binding

Now focus on the handle data body and, in particular all that optional checks inside the loop:

We have many lines as:

Let’s define a function:

function explained: if ao (any object) is not nil and can be typecasted to String return the String, otherwise return null.

Then we can write the previous lines simply as:

Define it also for Doubles:

and then our optionals check becomes:

Little better, but the story is not finished. In the next posts we are going to play with “Refactoring” and “Custom operators”.

Here is code of Part 2: Gihub project

Swift JSON Parser – Part 3

Valerio Ferrucci

Valerio Ferrucci (valfer) develops software on Apple Macintosh since the 1990s until today for MacOS, OSX and, since some years, iOS. He is also a Web (PHP/MySQL/JS/CSS) and Android Developer.

More Posts - Website

Follow Me:
TwitterFacebookLinkedIn

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Leave a Reply

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