Block Syntax (1)

Source: Internet
Author: User

1. Why block is used.

We know that there are three ways to communicate between objects and objects: 1, Agent-agreement, 2, notice, 3, block. There are three ways to achieve the decoupling between objects. The difference is: the communication mode of notification is 1 to many, the agent, Block communication mode is 1 to 1.

Introduction to 2.Block.
    • Block is a new syntactic structure after iOS4.0, and also becomes a "closure".
    • SDK4.0 New API heavily uses block
    • Block is an anonymous function code block that can be passed as a parameter to other objects.

3. Use of block
// int is the return value; Sumblock is the block variable type; (int x,int y) The parentheses are the parameters.  int(^sumblock) (int x,int y);

 

1 //Create Myblock2- (void) Createmyblock {3     //defining a block variable4     int(^sumblock) (intXinty);5     //implement block, and assign values6Sumblock = ^ (intXinty) {7         returnX +y;8     };9     //declares the Myblock variable, global {Int (^myblock) (int x,int y);}
Ten     Myblock = Sumblock;  One     NSLog (@ "%d", Myblock ());  --}
4. Create block

Block has the following 4 ways:

1 //block is an anonymous function and cannot be called by function name, so it doesn't make sense to use it alone2 //1. No parameter no return value--the same effect as the direct printing data, meaningless3^(){4NSLog (@"No parameter no return value");5     }();6     7 //2. No return value with parameters8^(inta,nsstring*text) {9NSLog (@"%d \%@", a,text);Ten}(Ten,@"There are no return values for parameters"); One      A //3. There is no parameter for return value -nsstring* A = ^(){ -         return @"no parameter has return value"; the     }(); -NSLog (@"%@", a); -      - //4. Parameters have a return value +     //defining a block variable -     int(^sumblock) (intXinty); +     //implement block, and assign values ASumblock = ^ (intXinty) { at         returnX +y; -     }; -NSLog (@"%d", Sumblock (Ten,Ten)); -}
1 //The parameter has a return value (The return result is 6)2     int(^powefblock) (intXinty);3Powefblock = ^ (intXinty) {        4         returnX *y;5     };6NSLog (@"powef =%d", Powefblock (2,3));
5. Bloc and variables
    • Variables can be divided into global variables and local variables by function.
    • These two variables can be used in a block, but there are different management mechanisms.

Referencing local variables

    • When a block refers to a local variable, the change amount is encoded as a constant in block blocks.
    • Local variables need to be modified in block by using the __block modifier.
1 //If an external variable is used inside the block code, number becomes constant2     intNumber =Ten;3^(){4       //Number = 20; Direct Error---Can not be modified, has become constant5NSLog (@"%d", number);6     }();7 8 //-- changes in the interior-9 //add a __block declaration before the variableTen__blockintnumber_1 =Ten; One^(){ Anumber_1 = -; -NSLog (@"%d", number_1); -}();
    • referencing external variables
#import <Foundation/Foundation.h>@interface  myobject:nsobject{        // block references external member variables    int _number;} -(void) log; @end
1- (void) Log {2    3 //block references external member variables4_number = About;5NSLog (@"---%ld--", Self.retaincount); 6 //use block to modify the Obj object so that obj is used in block and is not retain. 7 //obj cannot have ownership of self block does not hold self object8__block myobject* obj =Self ; 9^(){Ten         //determine if obj exists One         if(!obj) { A             return ; -         } -         //gets the member variable through obj; the print result is 1. the         intA = obj_number; -NSLog (@"---%d--", a); -NSLog (@"*****%ld****", self.retaincount); -     }(); +      -}
6. Block's memory management
    • When a local Objective-c object is referenced within a block, the object is retain.
    • If the local variable uses the __block adornment, it will not be retain.  
1 //--------Reference to an external obj object----------2 //will not be retain, i.e. Obj1.retaincount is always 13 //__block nsobject* obj1 = [[NSObject alloc] init];4 //Obj1.retaincount will change to 2 after the delay call5nsobject* obj1 =[[NSObject alloc] init];6NSLog (@"%ld", obj1.retaincount);7     8     //Delay Call9Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (2* nsec_per_sec)), Dispatch_get_main_queue (), ^{        TenNSLog (@"%ld", obj1.retaincount); One});
    • Block itself can copy and release as objects
    • After the block is created, the memory is allocated on the stack, and after the copy method is called, the block is moved to the heap.
    • When the block is declared as a global variable, the block's copy method should be called.

7. Circular References
    • When referencing an instance variable in block (dot syntax), the instance variable is retain.
    • such as the rules can easily lead to circular references, resulting in

Readers are welcome to check, if reproduced, please indicate the source.

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