Understanding of the iOS-Block

In OC, the so-called block: is a block of code, we can be understood as a similar method.

About Block Memory Management:

Block can say that as a method, the so-called method, is invoked from the hard disk into memory, and then to execute code means executing the disappeared; so we do not need memory management method, that is, the method is the stack area in memory.

Similarly, the block unlike the class object OC (OC class object is stored in the stack area), are also present in the block stack area; when we use the block as a property of an object, which we will use modified Copy, because under normal circumstances block in the stack area, we can not control its demise; when we use the modified copy, a copy of the system will realize that a block of the heap, so we corresponding properties, you own the block ownership, you can ensure that block of code block will not die early.



PS: Block the course of things should be noted

I realized at the time of the block, might use the external variables block (block outside the braces); we know that local variables (non-static) can not be used outside, and block and a similar method, why can block use external variables?

In fact: This is because OC is a runtime language, we write the code OC will eventually be converted into C code language to perform; we can find the code by running, the system will use the external variables passed to the parameter list to block, it becomes a local variable inside a block, it can be used.

However, during the passed parameters.

For external data basic data types, this time the system default delivery only value and can not be modified on the fundamental parameters, that can not modify these parameters can only be read; if you want to modify these external variables, this variable will have to be modified to use __block; As a result, when we pass parameters, not just pass the value of external variables, but the delivery address external variables, we can change these external variables worth it.

__block int a = 10; // with the after __block modified, a transmission system address (& a)

    _block = ^{

        a +=20;

        NSLog (@ "a =% d", a); // the address, of course, can modify the value of a. At this time, the value of a is 30

};

For external variable object type, this time the system default delivery address is passed; while default once strong reference to the object. System was strong references, and regardless of the block and memory management (only the strong references, and do not release operation), this time it will cause a memory leak. So, when we use the object, at MRC, will use __block modified under ARC, use __weak modified, so that, at the time of the transfer system will not be strong reference to the object to avoid memory leakage.

- (void)viewDidLoad { 

[super viewDidLoad]; 

UIView *view = [[UIView alloc] init]; 

__weak typeof (view) _view = view; // _ view and a view point to the same memory, but is weak references _view, or view the retainCount 1. 

_block = ^{ 

//view.frame = CGRectMake (0, 0, 100, 100); // use within the view object Block, the system will view strong reference, this time will be a memory leak.

_view.frame = CGRectMake(0,0,100,100);

 };

}

Above reference links

Guess you like

Origin blog.csdn.net/weixin_34090643/article/details/90991557
Recommended