What is the difference between __weak and __block modifiers?

Source: Internet
Author: User

The API reference has the following explanations for the __block variable modifier:

A powerful feature ofBlocks is that they can modify variablesinch  theSame lexical scope. You signal thataBlock can modifya variable using  the__block storage type modifier.//at function level is __block variables. These is mutable within  theBlock ( and  theEnclosing scope) andis preservedif  anyReferencing block is copied to  theHeap.

The approximate meaning comes down to two points:
The 1.__block object can be modified and re-assigned in the block.
2.__block objects are not strongly referenced by block in block, so there is no circular reference problem.

The API reference has the following explanations for the __weak variable modifier:

a referencethatdoesnotthereferenceissettotothe object.

An object that uses the __weak modifier is equivalent to the property defined as weak. Nature does not cause circular reference problems, because the Apple documentation has made it clear that when the original object does not have any strong references, the weak reference pointer is also set to nil.

Therefore, the difference between the __block and __weak modifiers is actually quite obvious:
1.__block can be used in either ARC or MRC mode, can be decorated with objects, and can also be decorated with basic data types.
2.__weak can only be used in Arc mode, can only be decorated with objects (nsstring), and cannot be decorated with basic data types (int).
The 3.__block object can be re-assigned in the block, __weak not.
The ps:__unsafe_unretained modifier can be considered an alternative to iOS SDK version __weak, but will not be automatically empty to nil. So do not use this modifier as much as possible.

What is the difference between __weak and __block modifiers?

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.