Oc--block

Source: Internet
Author: User

Block is used to save a piece of code
Block's logo: ^
Block is like a function:
1. Can save the Code
2. There is a return value
3. Tangible parameters
4. Call the same way

Defining a block variable

void (^myblock) () = ^{
NSLog (@ "----------------");
NSLog (@ "----------------");
};

Use the block variable to invoke the code inside the block;
Myblock ();

Int (^sumblock) (int,int) = ^ (int a, int b) {
return a + B;
};

int c = Sumblock (10,11);

/*
pointer function pointer
Int (*p) (int,int) = sum;
int d = p (10,12);
NSLog (@ "%d", d);
*/

/*
What the block needs to know.
1> How to define a block variable
Int (^sumblock) (int, int);
void (^myblock) ();

2> How to use block encapsulation code
^ (int a, int b) {
return a-B;
};

^() {
NSLog (@ "----------");
};

^ {
NSLog (@ "----------");
};

3> block access outside variables
* External variables can be accessed inside the block
* By default, the outside local variables cannot be modified inside the block
* Add __block keyword to local variable, this local variable can be modified inside block

4> defining block types with typedef
typedef int (^myblock) (int, int);
You can then use this type of Myblock to define the block variable
Myblock Block;
Myblock B1, B2;

B1 = ^ (int a, int b) {
return a-B;
};

Myblock B3 = ^ (int a, int b) {
return a-B;
};
*/

Oc--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.