Block IOS block

Source: Internet
Author: User

Block is a strange thing. Not learned before, now look at it, still feel very strange.

Helpless, then the bullet-brace learned.

have parameter return value

Format:

return value type (^ variable name) (parameter type and number) = ^ (formal parameter list) {

code block statements;

Return

};

eg

Int (^plus) (int a,int b) = ^ (int a,int b) {

return a+b;

};

NSLog (@ "%d", plus (2,3));

1. The return value type is int;

2. Variable name is plus;

3. The parameter type is int, the number is separated by commas;

4. In the back, the formal parameters are to be written separately;

5. Add the statement to be written in curly braces;

6. End with a semicolon;

7. When called, add () after the Block method.

This is the equivalent of the formula. Before superficial understanding, now beg also not to pass.

For a block with no return value, there is no return value, which can be divided into parameters and no parameters.

Simply to execute the statement inside. That's how I understand it.

The parameter has no return value:

Format:

void (^ variable name) (parameter type and number) = ^ (formal parameter list) {

code block statements;

};

eg

void (^MYBLOCK1) (int,int) = ^ (int a, int b) {

NSLog (@ "a+b=%d", a+b);

};

(MYBLOCK1);

No parameter has a return value

Format:

void (^block variable name) () = ^ () {

The statement of the code block;

};

Optimization:

void (^block variable name) () = ^{

code block statements;

};

void (^MYBLOCK4) () =^{

NSLog (@ "I'm fine");

};

After summing up the above, I feel a lot clearer. The values are also used in the UI. Later in the work to be more summary, more practice.

Light says do not practice false bashi.

Tween

typedef forgot to write.

typedef, in fact, defines the block as a type. This is much more convenient when there are many different variables for the same type.

typedef void (^block) ();

----equivalent to defining a block type. We can arbitrarily define this type of variable.

Block B1 = ^{

NSLog (@ "HelloWorld");

};

B1 ();

Defines the block type with parameter/return value

Define a new Alias

The return value is int, with two arguments of type int

typedef int (^newtype) (int,int);

NewType New1 = ^ (int a,int b) {

return a+b;

};

Define variables for multiple Newtype types consecutively

NewType new2,new3,new4;

New1 = ^ (int x,int y) {

Return x>y?x:y;

};

int s = new1 (2,3);

NSLog (@ "s =%d", s);

Relatively speaking, it is very convenient and fast.

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