The use of block--a probe into block

Source: Internet
Author: User

Saw the block for two days. There are a lot of lectures and tutorials on the internet, and there are very good speakers. Here is the main explanation of my understanding and harvest, welcome to shoot bricks.
So-called block, I think it should be a closure function. The closure, that is, the block and all the variables within the life cycle of its invocation function. Once it is called, it will be released immediately. The operating mechanism is not described here, but only briefly on the main usage (in this case, the arc-based environment).

Define and use 1, block within the function body: life cycle: exists only in the function body, similar to local variables in the function body.

Function:

- (void)printBlock:(NSString *)output {    NSLog(@"output == %@", output);}

Block is placed directly in the form of the function body:

void(^printBlock)(NSString *) = ^(NSString *output){    NSLog(@"output == %@", output);};

As you can see here, the general form of our function body is:

- (返回值) 函数名: (形参)...

The invocation form is:[调用对象 函数名(形参)];

The block definition we put in the function body is:

Block名)( 形参 ) = ^( 形参 ){ //实现内容 }

The invocation form is:Block名(形参);

The block here is the equivalent of an inline function. Declarations and implementations are put together at the same time. The form is similar to the assignment of the object we normally use:id obj = ... ;

2. Block placed outside the function. is the global block within the scope of the class. The life cycle is internal to the entire class.
定义与使用方法都与上面一致,只不过就像局部变量与类变量一样是否可被其他方法调用而已。为了更简单明了我们可以看一下这个代码:
    //普通方法定义    - (void)printBlock:(NSString *)output {        NSLog(@"output == %@", output);    }    //函数体外的Block    void(^printBlock)(NSString *) = ^(NSString *output)    {      NSLog(@"output == %@", output);    };    //Block调用    - (void)printSomething {       printBlock(@"I‘m printBlock");    }
3. Block that is placed in the class definition header file. Can be owned by an instance object. The life cycle is the entire instance object.

Definitions in the header file (1):

typedefvoid(^printBlockClass)(NSString *);@interface BlockClass : NSObject@property (nonatomic, copy)printBlockClass print;@end//或者在实现文件中://但这种实现在内部的Block我也不知道有什么卵用。实际应用不大。@interface BlockClass : NSObject{    printBlockClass print;}@end

Definitions in the header file (2):

//与方式一的区别无非像是自定义一种结构然后你再去使用一样@interface BlockClass : NSObject@property (nonatomicvoid(^printBlockClass)(NSSting *output);@end
这种把Block作为属性的情况是比较多用于页面传值中。关于Block的使用还有一下几个注意点:1、只有在调用Block的时候才会执行{}内部的代码(就比如把Block定义在函数体内不调用是不会执行的)。2、在Block的{}内部代码中是对外部的变量可以引用,但不能改变。如果试图更改会产生一个编译器错误。除非你把想要更改的变量声明时加上 __block关键字。3、在block使用过程要注意不要产生循环引用。

About circular references (Memory Recycle) Here is an article that is well written. Interested to see: http://blog.csdn.net/fengsh998/article/details/38090205

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

The use of block--a probe into block

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.