$\Huge \color{brown}\epsilon$ ObjectiveC Block Example


//=============================================================================
// How to compile:
// Assume block.m is in the current directory
//=============================================================================
// clang -fobjc-arc -framework Foundation block.m
//=============================================================================
int main(int argc, const char* argv[])
{
    // 1. simple block example
    void (^simpleBlock)(void);
    
    simpleBlock = ^{
        NSLog(@"This is Block Objective-C tutorial \r\n");
    };
    
    simpleBlock();
    
    // 2. a bit more complicated block example 
    int (^returnBlock)(int, int);
    
    returnBlock = ^ int (int x, int y){
        return x + y;
    };
    
    // 3. access non-local variables
    double pi = 3.1415;
    double radius = 10.0;
    double (^area)(double radius);

    area = ^double (double radius){
        return pi*radius*radius;
    };

    pi = 10000.0;
    // 4. modify the non-local variable
    __block int count = 0;
    int (^increase)(void) = ^{
        return count++;
    };

    int(^index)(int, int, int) = ^(int col, int row, int width){
        return col*width+row;
    };
}