I am parsing a JSON file to build an object graph. While deserializing the JSON, I validate the objects using blocks. I need to break out of further block processing if any validation error happens on any of the objects. Is there a way to break out of nested blocks – like break in a switch statement?
I’m running iOS 5.0 and using ARC. Please let me know if you would like some code to help understand my problem.
The solution to this is to add a __block BOOL ivar to your class, lets call it ‘cancel’. When you find an error, set ‘cancel’ to yes. In every block, in every loop, only do work if ‘cancel’ is false.
This way, when the error is hit and cancel is set, the blocks will essentially quit as soon as they detect ‘cancel’ is set.
EDIT: since I wrote this ivars do not need the __block qualifier (and may never have needed it, not sure). When blocks reference ivars, they do it through a “self” pointer, i.e. self->ivar.