[Reading notes]ios with OS X multithreading and memory management [blocks section-1]

Source: Internet
Author: User

Chapter II Blocks
2.1 Blocks Abstract
Blocks is an extension of the C language and is an anonymous function with automatic variables (local variables). Blocks save the value of the automatic variable. Blocks is not a unique concept for objective-c, and it has a corresponding concept in other languages, such as Smalltalk or Ruby.
2.2 Blocks Mode
2.2.1 Block Syntax ^return value type parameter list An expressionThe return value type can be omitted ^parameter list An expressionParameter list can also be omitted ^An expression
The 2.2.2 block type variable can assign the block syntax to a variable declared as a block type. Such as:Int (^ablock) (int) =^ (int count) {return count+1;};You can also assign a value to a block type variable by a block type variable, such asInt (^bblock) (int) =ablock;The block type variable can be used as a function parameter, such asvoid func (int (^block) (int)) {}Block type variable as function return value:Int (^func ())) (int) {return ^ (int count) {return count+1;};}A block definition is similar to a function pointer definition, and is understood as a function return value in the C language:
Pointer to function: You can follow the definition of the underlying data type above for example:void (*p) ()=function; (Funciton is a well-defined function) format:function Type * pointer variable name= function name; Note: Since the P pointer is a pointer to(*p)() The red parentheses must be there, because the function itself is to be parameterized, even if no arguments are written(); If the function p points to has parameters:Int (*pp) (int, int) = SUM;Call pointer to function: no parameter: P (); parameter: int sum = PP (2,3), block is defined and used, and pointer to function is formatted much like for example:void (^firstblock) () = ^(){...   Code ...}; Description: The blue parenthesis can not be written, the parameters must be written when Format:Block Type^block name= ^{Code snippet}; parameter block:Int (^multiblock) (int, int) = ^ (int a, int b) {return a * b;};Call BLOCK: no parameter:  Firstblock () has the parameter: int result = Multiblock (2, 3);
function pointers as function return values:
void (*fun1 (double d)) (intchar) {    printf ("%f\n", D);     return Fun ;}

Using typedef makes the block more concise when it is used.typedef int (^blk_t) (int);We can compare:void func (int (^block) (int)) {}-void func (blk_t blk) {}Int (^func ())) (int) {}-blk_t func () {}
Calling the Block method is similar to calling a C language function. A variable of block type can be used exactly like a C language variable, so you can use a pointer to a block type variable, which is a block pointer type variable.blk_t blk=^ (int count) {return count+1};blk_t *blkptr=&blk;(*BLK_PTR) (ten);
2.2.3 Intercept Automatic variable values
int temp= 2;     void (^MYBLK) (void)=^(){printf("%d", temp);}; temp=; myblk ();The result is that the 2;block expression intercepts the value of the automatic variable (local variable) used.
2.2.4 __block specifierblock can only intercept the instantaneous value of the automatic variable, and cannot change the value of the automatic variable. To assign a value to an automatic variable declared outside the block syntax in an expression in block, you need to append the __block specifier to the variable, which is called the __block variable;
  __block int temp=2;
void ( ^myblk) () =^ () {
temp+=8;
};
Myblk (); printf("----%d", temp); The value of an automatic variable intercepted by 2.2.5 can not be modified, but when an OC object (such as Nsmutablearray) is intercepted, it is possible to add elements to the array, and the direct assignment is not. That is, it can be used and cannot be assigned. To assign a value, you also need to add the __block modifier. When using C-language arrays, it should be noted that the method of intercepting automatic variables does not implement interception of C-language arrays. As in the following code:
ConstChar text[]="Zyb";
void(^BLK) (void)=^{
printf("%c", text[2]);    };
An error is indicated: cannot refer to declaration with an array type inside block. You can use the pointer to resolve the problem:ConstChar *text="Zyb";
void(^BLK) (void)=^{
printf("%c", text[2]);};


[Reading notes]ios with OS X multithreading and memory management [blocks section-1]

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.