Objective C code block blocks complete summary Two

Source: Internet
Author: User

1. Block base block declaration a bit like a C-language function pointer

C code 
    1. int func (int);
    2. Int (*pfunc) (int);
    3. int func (int p)
    4. {
    5. printf ("%d", p);
    6. return p;
    7. }

where Func is a function, Pfunc is a function pointer
function pointer assignment Pfunc = &func;
function pointer use (*PFUNC) (100);
The declaration of Block Int (^bfunc) (int);
The assignment of block bfunc = ^ (int p) {printf ("%d", p); return p; };
Use of block Bfunc (10);
In some cases, users need to use a block with the same signature in multiple places, so a typedef can be used to define the block type, such as

C code 
    1. typedef INT (^myblock) (int);
    2. Myblock Block1 = ^ (int p) {
    3. printf ("%d", p);
    4. return p;
    5. }
    6. Myblock Block2 = ^ (int p) {
    7. printf ("%d", p * 2);
    8. return p * 2;
    9. }

2. Block's use of external variables is the same for global variables, static variables, class member variables, but for local variables, after the block is defined, its value is fixed, even if it is modified before the block itself is called. Local variables used inside the block are still values when the block is defined
Here is the sample code, where VAR1 is a global variable, VAR2 is a global static variable, VAR3 is a member variable of the class, and VAR4 is a local variable of the function.

C code 
  1. typedef VOID (^myblock) (int);
  2. ....
  3. var1 = 1;
  4. Myblock Block1 = ^ (int p)
  5. {
  6. printf ("\nvar1:%d", var1);
  7. };
  8. Block1 (0);
  9. VAR1 = 2;
  10. Block1 (0);
  11. VAR2 = 2;
  12. Myblock Block2 = ^ (int p)
  13. {
  14. printf ("\nvar2:%d", var2);
  15. };
  16. Block2 (0);
  17. VAR2 = 3;
  18. Block2 (0);
  19. VAR3 = 3;
  20. Myblock Block3 = ^ (int p)
  21. {
  22. printf ("\nvar3:%d", VAR3);
  23. };
  24. Block3 (0);
  25. VAR3 = 4;
  26. Block3 (0);
  27. VAR4 = 4;
  28. Myblock Block4 = ^ (int p)
  29. {
  30. printf ("\nvar4:%d", VAR4);
  31. };
  32. Block4 (0);
  33. VAR4 = 5;
  34. Block4 (0);

The result of this code execution is: var1:1 var1:2 var2:2 var2:3 var3:3 var3:4 var4:4 var4:4
For global variables, static variables, and class member variables, the code in the block can modify their values. But for local variables, block treats it as a constant by default, and if you need to modify it, you must add a modifier to the local variable definition __block
3. Block and Objective-c object variables
For global variables and static variables, there is no special place compared to normal variables. According to Apple's official documentation, references to member variables and local variables in the block can cause retaincount changes. A direct reference to a member variable causes the object that contains the member variable to be Retaincount + 1, and a reference to the local variable causes the local variable to Retaincount + 1. If you use __block as the modifier for a local variable, you can make the local variable referenced by the block without retain operation.
I tried it myself and found that a little bit of Apple's documentation did not make it clear that block's reference to member variables and local variables did not necessarily cause retaincount changes, and here is my test code.
OBJ3 is a member variable

C code 
    1. OBJ3 = [[Testobjectalloc] init];
    2. printf ("\nself retain count:%d", [Selfretaincount]);
    3. Myblock block6 = ^ (int p)
    4. {
    5. printf ("\nself retain count:%d", [Selfretaincount]);
    6. printf ("\nobj retain count:%d", [Obj3retaincount]);
    7. };
    8. BLOCK6 (0);
    9. printf ("\nself retain count:%d", [Selfretaincount]);

Output: Self retain count:1-retain count:1 obj retain count:1 self retain count:1

C code 
  1. OBJ3 = [[Testobjectalloc] init];
  2. printf ("\nself retain count:%d", [Selfretaincount]);
  3. Myblock Block7 = ^ (int p)
  4. {
  5. printf ("\nself retain count:%d", [Selfretaincount]);
  6. printf ("\nobj retain count:%d", [Obj3retaincount]);
  7. };
  8. Myblock block77 = block_copy (BLOCK7);
  9. Block7 (0);  //or block77 (0);
  10. printf ("\nself retain count:%d", [Selfretaincount]);
  11. Block_release (block77);
  12. printf ("\nself retain count:%d", [Selfretaincount]);

Self retain count:1-retain count:2 obj retain count:1 self retain count:2 self retain count:1
OBJ4 is a local variable

C code 
    1. Testobject *OBJ4 = [[Testobjectalloc] initwithvalue:4];
    2. Myblock block8 = ^ (int p)
    3. {
    4. printf ("\nobj4 retain count:%d", [Obj4 Retaincount]);
    5. };
    6. Myblock block88 = block_copy (BLOCK8);
    7. block88 (0);
    8. printf ("\nobj4 retain count:%d", [Obj4 Retaincount]);
    9. Block_release (block88);
    10. printf ("\nobj4 retain count:%d", [Obj4 Retaincount]);

Obj4 retain count:2 obj4 retain count:2 obj4 retain count:1

C code 
    1. __block testobject *obj5 = [[Testobjectalloc] initwithvalue:5];
    2. Myblock Block9 = ^ (int p)
    3. {
    4. printf ("\nobj5 retain count:%d", [Obj5 Retaincount]);
    5. };
    6. Myblock block99 = block_copy (BLOCK9);
    7. block99 (0);
    8. printf ("\nobj5 retain count:%d", [Obj5 Retaincount]);
    9. Block_release (block99);
    10. printf ("\nobj5 retain count:%d", [Obj5 Retaincount]);

Obj5 retain count:1 obj5 retain count:1 obj5 retain count:1
The conclusion is that a block reference to an object within a method does not cause Retaincount to change, but after calling Block_copy, the Retaincount will change, but the retaincount of the referenced object will not increase after the block has finished executing. You need to call Block_release to release the retaincount increase that was caused before block_copy.

Objective C code block blocks complete summary Two

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.