__weak and __block differences

Source: Internet
Author: User

Blocks understand:

Blocks can access local variables, but cannot modify

If you modify a local variable, you need to add __block

__block int multiplier = 7;

Int (^myblock) (int) = ^ (int num) {

Multiplier ++;//, that's it.

return num * multiplier;

};

2, if the local variable is an array or pointer when only copy the pointer, two pointers to the same address, block only modify the contents of the pointer. Such as:

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);

The example does not modify marraycount this local variable AH. Marraycount is a pointer to a variable-length array. Inside the block, the pointer is not modified, but the array that the pointer points to is modified. In other words, Marraycount is an integer that holds the address of an area of memory, and in the block, does not change the address, but instead reads out the address and then operates on the contents of the address space.

This is allowed because when the block is declared, it is actually copying a copy of the temporary variable, even if the copied variables are modified in the block, it does not affect the original variables outside. That is, the so-called closure.

But when the variable is a pointer, the block simply copies a copy of the pointer, and two pointers point to the same address. So, the changes in the block inside the pointer to the content are also valid outside the block.

__weak __typeof (&*self) weakself =self; Equivalent to

__weak Uiviewcontroller *weakself =self;

Why not __block? Because the instance variable of self is accessed by reference, Self is retain,block also a strong reference, causing a circular reference, with __week being a weak reference, and when Self is released, weakself is already equal to nil.

Extension: Nstimer note to avoid circular references, you need to find a suitable time and place to invalidate timer

In the context of a reference count, the object is retain by default when you refer to a Objective-c object in the block. When you simply reference an instance variable of an object, it is also retain. But object variables tagged with the __block store type modifier will not be retain

Note: In the garbage collection mechanism, if you use both __weak and __block to identify a variable, then the block will not guarantee that it is always valid. If you use block when implementing the method, the object's memory management rules are more subtle: also (__weak and __block:)

1. If you access an instance variable by reference, self is retain.

2. If you access an instance variable by value, then the variable will be retain

Original address: http://blog.csdn.net/leikezhu1981/article/details/45009123



The text/your monkey brother (Jane book author)
Original link: http://www.jianshu.com/p/ba50850ae0a5
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

__weak and __block differences

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.