I've decided to try to use blocks for control flow in Objective-C and am running into some issues with calling multiple blocks inline.
I've got an OOBoolean which is a wrapper to a BOOL primitive, and provides these methods:
+ (id) booleanWithBool: (BOOL) boolPrimitive;
- (id) initWithBool: (BOOL) boolPrimitive;
- (void) ifTrueDo: (void (^) ()) trueBlock
ifFalseDo: (void (^) ()) falseBlock;
- (void) ifTrueDo: (void (^) ()) trueBlock;
- (void) ifFalseDo: (void (^) ()) falseBlock;
I have no problem using this class like so:
OOBoolean* condition = [OOBoolean booleanWithBool: (1 + 1 == 2)];
id trueBlock = ^(){
NSLog(@"True.");
};
id falseBlock = ^(){
NSLog(@"False.");
};
[condition ifTrueDo: trueBlock ifFalseDo: falseBlock];
And I get a result of "True.". But I keep getting syntax errors when trying this instead:
OOBoolean* condition = [OOBoolean booleanWithBool: (1 + 1 == 2)];
[condition ifTrueDo:(void (^)()) {
NSLog(@"True");
} ifFalseDo:(void (^)()) {
NSLog(@"False");
}];
Is it not possible to define multiple blocks anonymously and pass them to a method that takes multiple block arguments? If so, that's kind of a let down.
if() else
construct results in optimized program flow using certain options at compile time. – Hyperbole