Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it okay to pass in NULL to a block parameter?

iOS does not scream at me when I pass in NULL or nil to the completion block in animateWithDuration:animations:completion: but does that mean it's okay? Or is it better to open an empty ^{ }?

like image 350
pixelfreak Avatar asked Dec 19 '11 23:12

pixelfreak


1 Answers

This is okay as long as you can trust that the code to which you are passing the nil won't try to call it as a block.

A quick demonstration:

typedef void (^GenericBlock)(void);

void useThisBlock(GenericBlock block){
    block();
}

useThisBlock(^{NSLog(@"All okay.");});
useThisBlock(nil);    // Compiles but crashes

The inner code must check the block first: if( block ) block();

In the case of UIKit code, you should be fine.

like image 109
jscs Avatar answered Oct 24 '22 18:10

jscs