Some minor notes about block access to external variables

Source: Internet
Author: User

Some minor notes about block access to external variables

The Block type is a C-level syntax and operating mechanism. It is similar to the Standard C function, except that it contains executable code and variables bound to the heap and stack memory. Therefore, the Block object contains a set of status data, which is used to affect the behavior during program execution.

You can use Block to write some function statements that can be passed to the API, store them selectively, and use multiple threads. As a callback, Block is particularly useful because it contains both the Code during the callback and the data required during execution.

Since both Objective-C and C ++ are derived from C, the block is designed to be compatible with these three languages at the same time.

Today, we mainly talk about some things about block access to external variables!

1) You can access variables outside the block within the block.

External variables can be used inside the block.


Note that this is a new memory space variable.



You can also define a variable (local variable) with the same name as the external variable of the block. At this time, the local variable will temporarily block the scope of the external variable.


2) the block "external variables" cannot be modified inside the block"

Non-local variables are copied and stored in the block as the const variable, that is, they are read-only in the block. If you try to assign values to the m variable within the block, a compiler error is thrown.




Non-local variables in the freeze are a constant value, which is also a default security behavior-because it can prevent the code in the block from making unexpected changes to non-local variables.



Add local variables__blockKeyword, the local variable can be modified within the block. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20160408/20160408091645204.png" alt = "\">

Note:

1. Static and global variables are added and not added.__blockWill Directly reference the variable address. This means that the value of the variable can be modified. Add__blockKeyword.

2. Constant variables (NSString *a=@"hello";aIs a variable,@“hello”Is a constant .)
Do not add__blockType. block directly obtains the constant value (shallow copy ).
Add__blockType. block references the variable address. (For example, a variable, a = @ "abc". You can arbitrarily modify the content pointed to by .)

If you do not add__blockIf you modify the variable directly within the block, an error will be reported during compilation. The internal change volume of the block is read-only.


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.