IOS block use

Source: Internet
Author: User


#import "ViewController.h"
/*
One of the biggest benefits of using block is that you can access external variables at any time in your code block

For example, you declare a block of code in a method in the A.class class. You can call the member variables owned by A.class directly in the code block, and with certain conditions (__block), you can modify the values and pointers of these variables at any time.

Release a block member variable that you no longer need to use, just set it to nil

You can change the value of a local variable by adding a __block modifier to the local variable

Global variables, static variables and so on can be accessed at any time in the block, and their values and pointers are modified.

But it is also possible to use and modify the member variables and self of the classes directly in the block, but you need to be aware of circular references.

*/
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);
};
Parameter 10 for passing in int of block code block
MyBlock1 (10);
}
#pragma mark-block Usage 2 within a block, you can access global variables, static variables, and so on, and modify their values and pointers.
-(Ibaction) TouchBlock2: (ID) Sender {
/*
First, a weak reference to self is defined before the block wself, because it is a weak reference, so when self is freed wslelf becomes nil, and then references the weak reference in the block, considering the multithreaded case, by using a strong reference sself to reference the weak reference, if Self is not nil and self is retain to prevent self from being freed during subsequent use, and the strong reference sself is then used in subsequent block blocks, noting that the sself is nil detected before use because of a weak reference in a multithreaded environment wself When you assign a strong reference to a sself, the weak reference wself may already be nil.
In this way, the block does not hold the self reference, 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 (@ "<self> Dealloc before we could run this code.");
}
};
Passing parameters to block code blocks
ChangeValue (@ "Block2");
NSLog (@ "self.str2:%@", SELF.STR2);
}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

IOS block use

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.