2

For example I would see the following:

- (void) updateNotesView:(BOOL)visible animated:(BOOL)animated{

    void (^animations)(void) = ^{
        if (visible)
        {
            //do something
        }
        else
        {
            //do something
        }
     };


    void (^completion)(BOOL) = ^(BOOL finished){
        self.showingNotesView = visible;

        if (!visible)
        {
            //do something
        }
    };

}

I do not get the code that says :

void (^animations)(void) 

or

void (^completion)(BOOL) = ^(BOOL finished)

can someone please explain?

2
  • 5
    They're blocks. Basically a function wrapped in an object.
    – ughoavgfhw
    Nov 10, 2011 at 4:43
  • Blocks can be helpful in certain situations, usually ones that require an event to be triggered upon completion. UIPrintInteractionController has a completion handler built into its delegate for example. Or in animation chains, it can be used to trigger the next group or to just animate a single object.
    – CodaFi
    Nov 10, 2011 at 4:45

1 Answer 1

1

It's how you define a variable that will be a block or in some other cases function pointers like in c.

int (*pt2Function)(float, char, char) = NULL;   

if (pt2Function)
    pt2Function(5.0f, 'a', 'b');


 NSArray* array = [NSArray arrayWithObjects:@"a",@"b", nil];



void(^enumerateBlock)(id,NSUInteger,BOOL*) = ^(id obj, NSUInteger idx, BOOL *stop) {
    // some code
};

[array enumerateObjectsUsingBlock:enumerateBlock];

// OR

[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
   // inline block 
}];

// using a typedef for code neatness

typedef void(^typedefBlockVar)(id,NSUInteger,BOOL*);

typedefBlockVar myEnumrateBlock = ^(id obj, NSUInteger idx, BOOL *stop) {
    // some code
};

[array enumerateObjectsUsingBlock:myEnumrateBlock];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.