Blocks in objective-c are defined and used in many ways.
As Property
@property (nonatomicint (^myBlock)(intint b);
Block Code body:
_myBlock = ^int (intint b) { return a + b;};
Use:
sum = _myBlock(10, 20);
using typedef
typedefint (^MyBlock)(intint b);MyBlock myBlock = ^int (intint b) { return a * b;};
Use:
intsum = myBlock(1020);
As a variable
int (^myBlock) (intint b) = ^int (intint b) { return a - b;};intsum = myBlock(1020);
At this time Myblock can be passed as a variable freely, when called Myblock (ten); Can.
If you want to modify the current local variable in block, you need to use __block:
intsum0;int (^myBlock)(intint b) = ^int (intint b) { sum = a + b; returnsum;}
The block defaults to the local variable sum, which cannot be modified in case of a circular reference.
The __block object is not strongly referenced in the block, so there is no circular reference.
__block and __weak
It is known that when the block is declared, it simply copies the sum local variable, so if it is a pointer, it does not need to add __block to modify the contents of its point in the block.
__block modifies objects and basic data types, whereas __weak can only decorate objects.
The __block object can be modified (re-assigned) in the block and __weak not.
Therefore, for a class object, to modify its properties in a block, you need to use __weak. Such as:
count) { weakSelf.countcount;}
Parameters as method calls
Pre-declaration of Myblock and attribute MyBlock2,
typedefint (^MyBlock)(intint b);@property (nonatomic, copy) MyBlock myBlock2;
Defines the method Methodtakeblock receives Myblock.
- (int)methodTakeBlock:(MyBlock)block { intsum0; if (block) { sum = block(1020); } returnsum;}
When the method is called, the Myblock entity is implemented in its arguments:
sum = [self methodTakeBlock:^int (intint b) { return b / a;}];
This method is only available in implementation.
Specify the block type in the declaration of the method
In the interface:
// 方法调用的参数- (int)method2TakeBlock:(int (^) (intint b))block;
In the implementation:
- (int)method2TakeBlock:(int (^)(intint))block { intsum0; if (block) { sum = block(1020); } returnsum;}
Call Method:
sum = [self method2TakeBlock:^int(intint b) { return a * b - b;}];
Passing Data between Viewcontroller
Define a block in TestViewController.h to modify the label content in Viewcontroller when jumping from Testviewcontroller to Viewcontroller:
#import <UIKit/UIKit.h>@interface TestViewController : UIViewControllertypedefvoid(^BlockUpdateBtnTitle)(NSString *);@property (nonatomic, copy) BlockUpdateBtnTitle blockUpdateBtnTitle;@end
The block receives a nsstring parameter.
Click button to trigger the following action
- (IBAction)action:(UIButton *)sender { if (_blockUpdateBtnTitle) { _blockUpdateBtnTitle(@"value changed by block"); } [self dismissViewControllerAnimated:NO completion:nil];}
Pass the block entity in VIEWCONTROLLER.M, noting that its receive parameters are consistent with the definition:
- (IBAction)action:(UIButton *)sender { TestViewController *testVC = [[TestViewController alloc] init]; __weakself; testVC.blockUpdateBtnTitle = ^(NSString *btnTitle) { weakSelf.lb.text = btnTitle; }; [self presentViewController:testVC animated:NO completion:nil];}
Click button to jump to Testviewcontroller, execute the Blockupdatebtntitle in Testviewcontroller, and modify the contents of the label in Viewcontroller.
Because the properties in the Viewcontroller are modified in the block, you can use __weak to prevent circular references.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
IOS---Common ways to use block