Block 2 and Q &

Source: Internet
Author: User

I have briefly recorded some knowledge points about block before. Someone has asked a question in a reply recently. By the way, I feel that the content is a bit thin now.

Then, combine the questions and sort out related items.

Simply put, the local variables in the block cannot be modified, but why can an array be added? For example:

Nsmutablearray * marray = [nsmutablearray arraywithobjects:@"A",@"B",@"ABC", Nil]; nsmutablearray* Marraycount = [nsmutablearray arraywithcapacity:1]; [Marray enumerateobjectswitexceptions: nsenumerationconcurrent usingblock:^ (IDOBJ, nsuinteger idx, bool *Stop) {[marraycount addobject: [nsnumber numberwithint: [OBJ length];}];

I don't think this problem has much to do with block. The concept is not clear:

    1. Array and other objects in OC, is a pointer
    2. The pointer itself does not change when the array is added/deleted.

Therefore, any operation is feasible unless the array is assigned a value in the block.

The following summarizes the access permissions of variables under the Block:

    1. In the same scope, local variables are read-only in the block.
    2. Variables modified with _ block can be read and written in the same scope.
    3. Static variables and global variables can be read and written in the block.

When a local variable is used in the block, the block will save a partial variable on the stack (the block is stored on the stack), and the saved variable is a constant in the block, so it cannot be modified.

If it is an object in OC, blcok will retain and release the object after the execution is completed.

If a variable with _ block exists, the block can modify the variable. It can be seen that the variable with _ block is NOT thread-safe. In iOS, we often simplify the process by setting the request completionblock.CodeYou need to pay attention to this point.

The ability of block to store local variables under the same scope is similar to the closure feature of anonymous functions in JS. Let's look at an example:

Typedef Void (^ Testblock )( Void  );  Int Val = 20 ; Testblock block1 = ^ {Nslog ( @"  % D  "  , Val) ;}; Val = 50  ; Testblock block2 = ^ {Nslog ( @"  % D  "  , Val) ;}; Val = 5  ; Testblock block3 = ^ {Nslog ( @"  % D "  , Val) ;}; block1 (); block2 (); block3 ();  //  Output: 20 50 5 

Following the examples in most JS books, we also have:

 testblock block [ 10  ];   for  ( int  I =  0 ; I  10 ; I ++ ) {block [I] =^{ nslog (@"   the % d  "  , I) ;}; block [I] () ;}nslog ( @"   after: \ n  "  );   for  ( int  I =  0 ; I  10 ; I ++ ) {block [I] () ;} 

So pay attention to the block scope.

Block is essentially an object, so we have to mention memory management in OC.

 
IntLocalint =100; _ BlockIntBlockint =0; Testblock blockptr= ^{Blockint+ =Localint ;};

In the above Code, all variables are stored on the stack. Suppose we copy the block:

 
Testblock blockcopy = block_copy (blockptr );

For blockcopy, it also saves the local variable localint, but the localint is copied and stored in the heap. Similarly, blockint is copied and stored in the heap.

However, the blockint on the stack generates a pointer pointing to the blockint copied to the stack. In other words, blockint is a "shared" variable. Although the use of copy is rare, it is also worth attention.

In addition, the apple documentation also mentions:

If the block accesses the instance variables of the class, the block will retain self itself.

Because:

VaR: Self-> VaR

So self will be retain. In this case, loop reference may occur, so that the memory cannot be released. Be careful.

 

If something is wrong, please correct it.

 

 

 

 

 

 

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.