Usage of IOS block

Source: Internet
Author: User

The order of this article is layered in depth, to be simple to understand, only to see X.1 on the block, please.

Learning goals in this chapter:

1. Understand what block is.

2. Understand how block is used.

Block is an iOS new program syntax after 4.0, strictly speaking, the concept of block is not the scope of the basic program design, for beginners is not very easy to understand, but after iOS SDK 4.0, block appears in almost all the new API, in other words, If you don't understand the concept of block, you won't be able to use the new features of the SDK 4.0 version, so although the syntax of block itself is a bit difficult, we have to bite the bullet to understand the new programming concept in order to use the new features of iOS.

The goal in this chapter is to understand how to use block-based without delving into the workings of block bottom, as some beginners encounter fewer words such as "lexical scope (lexical scope)," and so on, this chapter will no longer explain, to interested readers to consult Google God.

X.1 on block

In this section we first use some simple examples to import the concept of block.

x.1.1 declaring and using block

We use the "^" operator to declare a block variable, and add ";" at the end of the block definition. To represent a complete statement (that is, to treat the entire block definition as a simple statement as described in the previous section, since the entire definition must be a complete sentence, and therefore must be preceded by a semicolon), here is an example of a block:

   1:int multiplier = 7;
   2:int (^myblock) (int) = ^ (int num)
   3: {
   4:     return num * multiplier;
   5:};

We use it to explain this example (please translate the words in the text box as follows):

We declare a "myblock" variable and use the "^" symbol to indicate that it is a block.

This is the complete definition of block, which will be assigned to the "myblock" variable.

Indicates that "myblock" is a block with a callback value of integer (int).

It has a parameter, and the pattern is also an integer.

The name of this parameter is called "num".

This is the contents of the block.

It is important to note that blocks can use the same variables that define the scope themselves, and you can imagine that in the example above, multiplier and myblock are all two variables defined within a function, and this variable is in the middle of a function two braces "{" and "}". Because their effective range is the same, the multiplier variable can be used directly in the block, and when the block is defined as a variable, we can use it directly as a general function:

Usage of IOS block

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.