Easiest would be to reverse the 'guard' condition, and use a simple if statement that exits, like thus:
if (!metadata) { return NO; }
Of course Obj-C compiler won't remind you that you must exit there, like the swift compiler.
One could #define a 'guard' macro that will automatically reverse the condition and also return after executing some parameter, but it can be tricky to write such macro in a general fashion, not knowing the type of returned value in advance.
#define GUARD(CONDITION,DO_BEFORE_EXIT) { if (!(CONDITION)) { DO_BEFORE_EXIT; return; }
and later, in your code:
GUARD(metaData!=nil, NSLog(@"ouch, nil metaData! exiting!");)
but there is also another old technique, with another benefit - unified handling of "final" code before exit, which is this:
-(void)myMethod {
do {
if (metadata==nil) break;
// do something
if (somethingelsefailed) break;
// do something else
// until done.
while (false);
NSLog(@"here put your final handling");
return;
};
Something I found more useful in C and Objective-C code than Swift guards.
( )
afterif
. Been doing too much Swift lately! – Paleolithic