Objective-C Language Blocks

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Syntax

  • // Declare as a local variable:

    returnType (^blockName)(parameterType1, parameterType2, ...) = ^returnType(argument1, argument2, ...) {...};

  • // Declare as a property:

    @property (nonatomic, copy, nullability) returnType (^blockName)(parameterTypes);

  • // Declare as a method parameter:

    - (void)someMethodThatTakesABlock:(returnType (^nullability)(parameterTypes))blockName;

  • // Declare as an argument to a method call:

    [someObject someMethodThatTakesABlock:^returnType (parameters) {...}];

  • // Declare as a typedef:

    typedef returnType (^TypeName)(parameterTypes);

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

  • // Declare a C function return a block object:

    BLOCK_RETURN_TYPE (^function_name(function parameters))(BLOCK_PARAMETER_TYPE);

Remarks

Blocks are specified by the Language Specification for Blocks for C, Objective-C, C++ and Objective-C++.

Additionally, the Blocks ABI is defined by the Block Implementation Specification.



Got any Objective-C Language Question?