OBJECTIVE-C Quick Start-Basic (iv)

Source: Internet
Author: User

1. What is block?

① block syntax, essentially an anonymous function (a function without a name);

②block is a data type in OC and is widely used in iOS development.

③^ is the unique mark of block;

The implementation code of the ④block is included in the {};

⑤ in most cases, the way inline functions are defined and used;

⑥block is somewhat similar to the C-language function pointer, but it is more flexible to use;

Example: In the main.m file:

  

Console output:

  

2. What is the block variable stored?

Simply speaking, the block variable is stored: the implementation of the function;

3. How do I define a block variable? How to assign a value to a block variable. If you now have an anonymous function int (NSString * a,int b), its function is to convert the number string A, to an integer value, and then add to B, return and value.

Example: In the main.m file:

  

Console output:

  

4. For the anonymous function of the above question, how to execute this anonymous function through the block variable implementation.

Example: In the main.m file:

  

5. Can I modify the value of a local variable in a block? Can I modify the value of a global variable?

① cannot, need to add _ _block when defining local variables;

② can modify global variables directly in block.

6. Create 3 student objects, which are stored in the array, and how the array is implemented in descending order of age by the block.

Example: In the main.m file:

  

Console output:

  

7, Void (^myblock) () = ^void () {NSLog (@ "Hello lanou!");}

Where: What is the type of the ①block variable?  What is the variable name of the ②block? What is the type of the value stored by the ③block variable?

Answer: ①void (^) () ②myblock③^void () {NSLog (@ "Hello lanou!");};

Email:[email protected]

OBJECTIVE-C Quick Start-Basic (iv)

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.