Blocks

Download objective-c eBook

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.

Related Examples

Stats

285 Contributors: 15
Wednesday, August 3, 2016
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook