IOS Block usage

Source: Internet
Author: User

IOS Block usage

 

# Import ViewController. h
/*
One of the biggest advantages of using Block is that you can access external variables at any time in the code Block.

For example. A code block is defined in a method in the class. you can directly. the member variables owned by the class are called, and the values and pointers of these variables can be modified at any time through certain conditions (_ block ).

Release a Block member variable that is no longer needed. You only need to set it to nil.

Add _ block to the local variable to change the value of the local variable.

You can access global variables and static variables at any time in the Block and modify their values and pointers.

However, you can directly use and modify the member variables and self of the class in the declared region in the Block.

*/
Typedef BOOL (^ block2) (void );

@ Interface ViewController ()
@ Property (strong, nonatomic) IBOutlet UIButton * block1;
-(IBAction) touchBlock1 :( id) sender;
-(IBAction) touchBlock2 :( id) sender;
-(IBAction) touchBlock3 :( id) sender;

@ Property (nonatomic, copy) NSString * str2;
@ End

@ Implementation ViewController

-(Void) viewDidLoad {
[Super viewDidLoad];
}
-(Void) didReceiveMemoryWarning {
[Super didReceiveMemoryWarning];
}
# Pragma mark-block usage 1
-(IBAction) touchBlock1 :( id) sender {
// Void is the return value of the code block, and int is the parameter of the code block.
Void (^ MyBlock1) (int I) = ^ (int I ){
NSLog (@ block = 1 = % d, I );
};
// Input the int type parameter 10 to the block code block
MyBlock1 (10 );
}
# Pragma mark-block usage 2 global variables and static variables can be accessed in the Block at any time, and their values and pointers can be modified.
-(IBAction) touchBlock2 :( id) sender {
/*
First, we define a weak reference wSelf for self before the block. Because it is a weak reference, when self is released, wSlelf will change to nil, and then reference this weak reference in the block, considering multithreading, sself is strongly referenced to reference this weak reference. In this case, if self is not nil, it will retain self to prevent the self from being released during subsequent use; then, use the strongly referenced sself in the block that follows. Note that sself should be checked for nil before use, in a multi-threaded environment, when wself is used to assign values to sself, the weak reference wself may already be nil.
In this way, the block will not hold the reference of self, thus breaking the circular reference.
*/
// Prevent circular references
_ Weak ViewController * wSelf = self;
// _ Weak typeof (self) weakSelf = self;
Void (^ changeValue) (NSString *) = ^ (NSString * str ){
WSelf. str2 = str;
ViewController * sself = wSelf;
If (sself ){
NSLog (@ % @, sself );
} Else {
NSLog (@ Dealloc before we cocould run this code .);
}
};
// Input parameters to the block code block
ChangeValue (@ block2 );
NSLog (@ self. str2: % @, self. str2 );
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.