Use of objective-C syntax Block

Source: Internet
Author: User

The code block is essentially similar to other variables. The difference is that the data stored in the code block is a function body. Using code blocks, you can pass in the number of parameters and get the return value just like calling other standard functions.

The Escape Character (^) is the block syntax mark. The returned values and the body of the block (that is, executable code) are defined according to the parameter syntax we are familiar ). The syntax for assigning block variables to a variable:

You can call the block object variable by calling the function:
Int result = myblock (4); // The result is 28

1. the parameter is the code block of nsstring *

 

[CPP]View plaincopy
  1. Void (^ printblock) (nsstring * X );
  2. Printblock = ^ (nsstring * Str)
  3. {
  4. Nslog (@ "Print: % @", STR );
  5. };
  6. Printblock (@ "Hello world! ");

The running result is: Print: Hello world!

 

2. The code is used in string array sorting.

 

[CPP]View plaincopy
  1. Nsarray * stringarray = [nsarray arraywithobjects: @ "ABC 1", @ "ABC 21", @ "ABC 12", @ "ABC 13", @ "ABC 05 ", nil];
  2. Nscomparator sortblock = ^ (ID string1, Id string2)
  3. {
  4. Return [string1 compare: string2];
  5. };
  6. Nsarray * sortarray = [stringarray sortedarrayusingcomparator: sortblock];
  7. Nslog (@ "sortarray: % @", sortarray );

Running result: sortarray :(

 

"ABC 05 ",

"ABC 1 ",

"ABC 12 ",

"ABC 13 ",

"ABC 21"

)

3. recursive call of code blocks

To call a code block recursively, the code block variable must be a global variable or a static variable. In this way, when the program starts, the code block variable is initialized and can be called recursively.

 

[CPP]View plaincopy
  1. Static void (^ const blocks) (INT) = ^ (int I)
  2. {
  3. If (I> 0 ){
  4. Nslog (@ "Num: % d", I );
  5. Blocks (I-1 );
  6. }
  7. };
  8. Blocks (3 );

Run the print result:

 

Num: 3

Num: 2

Num: 1

 4. Use local variables and global variables in the code block

You can use and change global variables in a code block.

 

[CPP]View plaincopy
  1. Int global = 1000;
  2. Int main (INT argc, const char * argv [])
  3. {
  4. @ Autoreleasepool {
  5. Void (^ block) (void) = ^ (void)
  6. {
  7. Global ++;
  8. Nslog (@ "Global: % d", global );
  9. };
  10. Block ();
  11. Nslog (@ "Global: % d", global );
  12. }
  13. Return 0;
  14. }

 

Run the print result:

Worldwide: 1001

Worldwide: 1001

Local variables can be used but cannot be changed.

 

[CPP]View plaincopy
  1. Int local = 500;
  2. Void (^ block) (void) = ^ (void)
  3. {
  4. Local ++;
  5. Nslog (@ "Local: % d", local );
  6. };
  7. Block ();
  8. Nslog (@ "Local: % d", local );

Changing the local variable compilation fails in the code block. How can I change local variables in a code block? Add the keyword __block before the local variable.

 

 

[CPP]View plaincopy
  1. _ Block int local = 500;
  2. Void (^ block) (void) = ^ (void)
  3. {
  4. Local ++;
  5. Nslog (@ "Local: % d", local );
  6. };
  7. Block ();
  8. Nslog (@ "Local: % d", local );

Running result: Local: 501

 

 

Locally: 501

 

Copyright Disclaimer: This article will be published at http://blog.csdn.net/totogo2010/, and you will be welcomed to enjoy the transfer and sharing. Please respect the work of the author. Keep this note and the author's blog link when reprinting. Thank you!

Use of objective-C syntax Block

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.