Difference between _ block and _ weak, __block _ weak

Source: Internet
Author: User

Difference between _ block and _ weak, __block _ weak

Blocks comprehension:

Blocks can access local variables, but cannot be modified.

If you modify a local variable, add _ block.

 

_ Block int multiplier = 7; int (^ myBlock) (int) = ^ (int num) {multiplier ++; // you can return num * multiplier ;};

 

 

2. If the local variable is an array or pointer, only the pointer is copied. The two pointers direct to the same address, and the block only modifies the content on the pointer. For example:

 

NSMutableArray *mArray = [NSMutableArray arrayWithObjects:@"a",@"b",@"abc",nil];    NSMutableArray *mArrayCount = [NSMutableArray arrayWithCapacity:1];    [mArray enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock: ^(id obj,NSUInteger idx, BOOL *stop){        [mArrayCount addObject:[NSNumber numberWithInt:[obj length]]];    }];       NSLog(@"%@",mArrayCount);

It is not modified in the example.mArrayCountThis local variable.mArrayCountIs a pointer pointing to a variable-length array. In the block, the pointer is not modified, but the array pointed to by the pointer is modified. In other words,mArrayCountIt is an integer that stores the address of a memory area. In the block, the address is not changed. Instead, the address is read and operated on the content of the address space.

This is allowed, because when block is declared, the temporary variables at that time are actually copied, even if the copied variables are modified in the block, it does not affect the external original variables. The so-called closure.

But when the variable is a pointer, the block just copies this pointer, and the two pointers point to the same address. Therefore, the changes made to the pointer to the content in the block also take effect outside the block.

 

_ Weak _ typeof (& * self) weakSelf = self; equivalent

_ Weak UIViewController * weakSelf = self;

Why not use the _ block parameter because the instance variable of self is accessed through reference. self is retain, and the block is also a strong reference, which causes circular reference. The _ week parameter is a weak reference, when self is released, weakSelf is equal to nil.

Extended: nstdate pay attention to avoiding circular references. You need to find a proper time and place to invalidate timer.

 

In the reference counting environment, by default, When you reference an Objective-C object in the block, the object will be retain. When you reference an instance variable of an object, it is also retain. But the object variables marked by the _ block storage type modifier will not be retain

Note: In the garbage collection mechanism, if you use _ weak and _ block to identify a variable at the same time, the block will not guarantee its validity. If you use block when implementing the method, the object's memory management rules are more subtle: Also (_ weak and _ block are different :)

1. If you access an instance variable through reference, self will be retain.
2. If you access an instance variable through the value, the variable will be retain

 

 

_ Weak mainly applies to avoid loop reference, how to avoid see blog: http://www.cnblogs.com/MasterPeng/p/5311911.html

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.