Block type

There are three types of blocks :__NSGlobalBlock,__NSStackBlock, and __NSMallocBlock

__NSGlobalBlock: A block that does not reference a variable or OC attribute is __NSGlobalBlock

__NSStackBlock: A block that references a variable or OC attribute is an __NSStackBlock

__NSMallocBlock: When an __NSStackBlock calls copy or a strong reference, it returns an __NSMallocBlock

Note: In an ARC environment, the compiler automatically copies blocks on the stack to the heap

Copies of the block

If it was already on the heap, increment the reference count by one

If it was on the stack, it is copied to the heap, the reference count is initialized to 1, and the copy Helper method is called (if it exists)

If the block is in the global area, the block itself is returned without reference counting or copying

The arg argument is the block_layout object

The return value is the address of the copied block

Copy trigger time

Manual Copy (MRC)

Modified by strong reference or copy

The system API contains usingBlock

code

#import "block.h" @interface block () @property(nonatomic,copy)void(^block)(void); | | | | | | | | | | | | | | | | | | (void)done{// loop through self->block->self //1.__weak: self = value; When no strong reference is released as nil __weak typeof(self) weakSelf = self; //2. If __NSMallocBlock captures self, self is copied on the heap. //3. WeakSelf now represents self NSLog(@"%@",weakSelf) removed from the weak reference table; __strong typeof(weakSelf) strongSelf = weakSelf; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(),^{ NSLog(@"%@",strongSelf); }); }; self.block(); } @endCopy the code