Introduction to Block in iOS (1) Basics and introduction to iosblock

Source: Internet
Author: User

Introduction to Block in iOS (1) Basics and introduction to iosblock
Introduction to Block in iOS (I) Basics I. Overview

Block is a C-level syntax and runtime feature. Block is similar to a C function, but the flexibility of Block is reflected in the reference of stack memory and heap memory. We can even pass a Block as a parameter to other functions or blocks.

Ii. Warm-up

Let's first look at a simple Block example:

  1. Int multiplier = 7;
  2. Int (^ myBlock) (int) = ^ (int num ){
  3. Return num * multiplier;
  4. };

In this example, myBlock is a Block variable that accepts an int type parameter and returns an int type value. Is it like a C function?

Come on, let's take a look at typedef.

  1. Typedef void (^ BoolBlock) (BOOL); // a block that only accepts one BOOL parameter and does not return a value
  2. Typedef int (^ IntBlock) (void); // a block with no parameters returned
  3. Typedef BoolBlock (^ HugeBlock) (IntBlock); // check whether the parameters and returned values of this HugeBlock are blocks.

Iii. More detailed examples

Note: The above typedef is still valid ~

Take the initiative to call:

  1. -(Void) someMethod
  2. {
  3. BoolBlock ablock = ^ (BOOL bValue ){
  4. NSLog (@ "Bool block! ");
  5. };
  6. Ablock ();
  7. }

Return as a parameter:

  1. Typedef void (^ BoolBlock) (BOOL );
  2. -(BoolBlock) foo ()
  3. {
  4. BoolBlock ablock = ^ (BOOL bValue ){
  5. NSLog (@ "Bool block! ");
  6. };
  7. Return [[ablock copy] autorelease]; // copy it to the stack. For more details, see the subsequent sections.
  8. }

A member of the class:

  1. @ Interface OBJ1: NSObject
  2. @ Property (nonatomic, copy) BoolBlock block; // The reason is the same as above. Students
  3. @ End
  4.  
  5. OBJ1 * obj1 =...
  6. Obj1.block = ^ (BOOL bValue ){
  7. NSLog (@ "Bool block! ");
  8. };

Parameters of other functions:

  1. -(Void) foo (BoolBlock block)
  2. {
  3. If (block ){
  4. Block ();
  5. }
  6. }

Other block parameters:

  1. BoolBlock bBlock = ^ (BOOL bV) {if (Bv) {/* do some thing */}};
  2. HugeBlock hBlock = ^ (BoolBlock bB) {bB ();};
  3.  
  4. HBolck (bBlock );

Ah, global variables! :

  1. Static int (^ maxIntBlock) (int, int) = ^ (int a, int B) {return a> B? A: B ;};
  2. Int main ()
  3. {
  4. Printf ("% d \ n", maxIntBlock (2, 10 ));
  5. Return 0;
  6. }

Okay, you know how to use the block.

4. Special Mark __block

If you want to modify the stack variable declared outside the block, add _ block to the variable:

  1. Int main ()
  2. {
  3. _ Block int I = 1024;
  4. BoolBlock bBlock = ^ (BOOL bV ){
  5. If (bV ){
  6. I ++; // if there is no _ block mark, it cannot be compiled.
  7. }
  8. };
  9. }

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.