how to create a block in objective c
define a block in .h file
/*here i created a block
addPhysicsBody is the name of block
^ without is block can’t be completed
so block is defined ^addPhysicsBody
typedef void (^addPhysicsBody)(CGPoint);
where void is return type and CGpoint is parameter
where bolded part is a parameter taken by block
2) step 2
@property (copy) addPhysicsBody clickBlock;
add block as a property and please remember that this property is copy
because a block needs to be copied to keep track of its captured state outside of the original scope by apple
define a function in .h file that takes block as a parameter this is necessary while creating block with function
- (void)getPhysicsBody:(void (^)(CGPoint))finishBlock;
where above bolded part is block with its parameter
- (void)getPhysicsBodyAtScene:(void (^)(CGPoint))finishBlock{
//block passed to self.clickBlock property
self.clickBlock=finishBlock;
}
4)step 4
pass block
pass parameter to block self.clickBlock
self.clickBlock(location);
note use above syntax where you want to fire the block
5)
capture block
[self getPhysicsBodyAtScene:^(CGPoint points) {
//
NSLog(@"%f",points.x);
}];
Comments
Post a Comment