Documenting Specific F%&cking Syntax For The Examples On f%&

While I am enjoying Swift more and more, I still love Objective-C, and continue to work in it professionally. One of the biggest syntactic pain points in Objective-C is definitely blocks (closures for the non Objc).

When trying to write an Objective-C block from scratch, the majority of iOS developers turn immediately to (This is not a secret) Due to the slightly off-putting syntax that is different in different contexts.

But I often find myself slowed down for a couple minutes trying to turn the generalized syntax (below) into specific syntax:

As a local variable:

returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...};

Since getting the precise form remains a compiler-aided exercise at times, I decided to finally write out (void <- nil) and (NSString * <- NSArray *) blocks in all of their various uses. It's definitely a little tedious, but I wanted to have a "go to" reference for this. Maybe someone else will find it helpful

//As a typedef
typedef void (^VoidReturnNoParamType)();
typedef NSString * (^StringReturnArrayParamType)(NSArray *);
typedef NSString * (^StringReturnArrayNamedParamType)(NSArray * _Nonnull array);

@interface BlockExample : NSObject 

//As a property
@property (nonatomic, copy, ) void (^voidReturnNoParamBlockProp)();
@property (nonatomic, copy, nullable) NSString * (^stringReturnArrayParamBlockProp)(NSArray *);
@property (nonatomic, copy, nullable) NSString * (^stringReturnArrayNamedParamBlockProp)(NSArray * _Nonnull array);

//As a method parameter:
- (void)methodWithNoReturnNoParam:(void (^ _Nullable)())voidReturnNoParamBlock;
- (void)methodWithStringReturnArrayParam:(NSString * (^ _Nullable)(NSArray *))stringReturnArrayParamBlock;
- (void)methodWithStringReturnArrayNamedParam:(NSString * (^ _Nullable)(NSArray * _Nonnull array))stringReturnArrayParamBlock;

//As a return value from a method:
- (void (^ _Nullable)())methodReturningBlockWithNoReturnNoParam;
- (NSString * (^)(NSArray *))methodReturningBlockStringReturnWithArrayParams;
- (_Nullable StringReturnArrayParamType)methodReturningBlockWithTypeDef;

And these are the uses of blocks, which is much easier to remember, but includes a couple nuances with regards to use of nullability:

    //local declaration of block
    void (^voidReturnNoParamBlock)() = ^() { NSLog(@"void return and no parameters"); };
    NSString * (^stringReturnArrayParamBlock)(NSArray *) = ^(NSArray *array) { return [array componentsJoinedByString:@"-"]; };
    NSString * (^stringReturnArrayNamedParamBlock)(NSArray * _Nonnull name) = ^(NSArray * _Nonnull array) { return [array componentsJoinedByString:@"-"]; };

    //Execute local block directly
    stringReturnArrayParamBlock(@[@1, @3, @4]);
    stringReturnArrayParamBlock(nil);       //no compiler warning
    stringReturnArrayNamedParamBlock(nil);  //compiler warning

    //Assign the block to an object's property with matching type
    example.voidReturnNoParamBlockProp = voidReturnNoParamBlock;

    //Execute block property directly

    //Passing block as an argument to a method call:
    [example methodWithNoReturnNoParam:voidReturnNoParamBlock];

    //Assigning a local block (defined with corresponding typedef) with a method that returns block
    VoidReturnNoParamType returnedVoidReturnNoParamBlock = [example methodReturningBlockWithNoReturnNoParam];

    //Execute that returned block

    //Calling a block returned from a method directly
    [example methodReturningBlockWithNoReturnNoParam]();

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s