iOS Review note 10: Using block

Source: Internet
Author: User

Function: Save a piece of code that can be executed at any time.
Flag: ^
Similar to a function:
1 can save a section of code
2 has a return value
3 Tangible parameters
4 Invocation Mode
Variable declaration: More function pointer variable similar
return value type (^ variable name) (parameter type list)


typedef
A block with a consistent return value and parameters can be a typedef of the same type


Permissions:
External variables can be accessed inside the block;
However, by default, the outer local variables cannot be modified inside the block;
This variable can be modified inside the block after the local variable is added to the __block keyword.




Example:
Main.m#import <foundation/foundation.h>typedef Int (^mulblock) (int, int)); int main () {//defines a block, and assign the value to the block variable block1void (^block1) = ^{//If there is no formal parameter, you can omit the parameter list NSLog (@ "This is a block");};/ /execute Blockblock1 (), int (^addblock) (int, int)), Addblock = ^ (int a, int b) {return a + B;}; Int (^subblock) (int, int)) = ^ (int a, int b) {return a-A;}; NSLog (@ "%d +%d", Addblock (2, 1), Subblock (2-1)); Mulblock add = ^ (int A, in B) {return a + B;}; Mulblock mul = ^ (int A, in B) {return a * b;}; return 0;}


iOS Review note 10: Using 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.