Block resolution-parameter variables

Source: Internet
Author: User
Block Parameters

I have missed an introduction to block parameters. I will add them here.

Block parameters are the parameters that define the block. They are used in the same way as the function parameters. We can modify the block parameters at will in the block.

Let's look at an example:

We declared two nsstring pointers: _ P1 _ P2, int _ P3, and variable array _ P4. We passed these parameters into the block and modified them in the block.

-(Void) test3 {nsstring * _ p1 = [nsstring stringwithformat: @ "Hello % @", @ "world11"]; nsstring * _ P2 = [nsstring stringwithformat: @ "Hello % @", @ "World12"]; int _ P3 = 1; nsmutablearray * _ P4 = [[nsmutablearray alloc] init]; // Initial Value nslog (@ "init P1: % @, % P, % P", _ P1, _ P1, & _ P1); nslog (@ "init P2: % @, % P, % P ", _ P2, _ P2, & _ P2); nslog (@" init P3: % d, % P ", _ P3, & _ P3); nslog (@ "init P4: % @, % P, % P", _ P4, _ P4, & _ P4); void (^ myblock) (nsstring *, nsstring **, Int, nsmutablearray *) = ^ (nsstring * P1, nsstring ** P2, int P3, nsmutablearray * P4) {// The value of P1 = @ "21" in the block; * P2 = @ "22"; P3 = 23; [P4 addobject: @ "23"]; nslog (@ "excuteing P1: % @, % P, % P", P1, P1, & P1); nslog (@ "excuteing P2: % @, % P, % P, % P ", * P2, * P2, P2, & p2); nslog (@" excuteing P3: % d, % P ", P3, & P3 ); nslog (@ "excuteing P4: % @, % P, % P", P4, P4, & P4) ;}; myblock (_ P1, & _ P2, _ P3, _ P4); // nslog (@ "excuteafter P1: % @, % P, % P", _ P1, _ P1, & _ P1) after the block is executed ); nslog (@ "excuteafter P2: % @, % P, % P", _ P2, _ P2, & _ P2); nslog (@ "excuteafter P3: % d, % P ", _ P3, & _ P3); nslog (@" excuteafter P4: % @, % P, % P ", _ P4, _ P4, & _ P4 );}

 

Output log:

2014-07-31 14:17:06.774 Test[3655:60b] init p1:hello world11,0x16537b20,0x27df89902014-07-31 14:17:06.776 Test[3655:60b] init p2:hello world12,0x1652d760,0x27df898c2014-07-31 14:17:06.778 Test[3655:60b] init p3:1,0x27df89882014-07-31 14:17:06.779 Test[3655:60b] init p4:(),0x1652b6d0,0x27df89842014-07-31 14:17:06.781 Test[3655:60b] excuteing p1:21,0x11a08,0x27df893c2014-07-31 14:17:06.782 Test[3655:60b] excuteing p2:22,0x11a18,0x27df897c,0x27df89382014-07-31 14:17:06.783 Test[3655:60b] excuteing p3:23,0x27df89342014-07-31 14:17:06.784 Test[3655:60b] excuteing p4:(    23),0x1652b6d0,0x27df89302014-07-31 14:17:06.785 Test[3655:60b] excuteafter p1:hello world11,0x16537b20,0x27df89902014-07-31 14:17:06.786 Test[3655:60b] excuteafter p2:22,0x11a18,0x27df898c2014-07-31 14:17:06.787 Test[3655:60b] excuteafter p3:1,0x27df89882014-07-31 14:17:06.788 Test[3655:60b] excuteafter p4:(    23),0x1652b6d0,0x27df8984

The log shows that:

  • We pass the pointer _ p1 (nsstring *) into the block. The parameter passing process can be understood as pointing the new pointer to the memory address pointed to by _ P1. In this case, the pointer pointing inside the block is modified, but the pointer pointing to the block parameter is modified. Therefore, the content of P1 inside the block is different from that of external _ P1.
  • We pass the pointer _ P4 (nsmutablearray *) into the block. The process of passing the parameter is the same as above. A new pointer is used to point to the memory address pointed to by the parameter, modify the memory pointed to by the shape parameter pointer within the block, that is, the operation is the same piece of memory, so it can be synchronized to the outside of the block.
  • We pass the pointer _ P2 (nsstring **) to the block and pass the pointer address. We modify * P2, in fact, it is to modify the memory address pointed to by the pointer P2, and it can also be synchronized to the outside of the block.
  • Because _ P3 is a value transfer, the internal modification of the block will not affect the external, which is the same as the function.

 

Let's take a look at how block processes the row-dependent variables:

This is a block struct and no member is generated to reference the parameter.

struct __KDBlockTest__test3_block_impl_0 {  struct __block_impl impl;  struct __KDBlockTest__test3_block_desc_0* Desc;  __KDBlockTest__test3_block_impl_0(void *fp, struct __KDBlockTest__test3_block_desc_0 *desc, int flags=0) {    impl.isa = &_NSConcreteStackBlock;    impl.Flags = flags;    impl.FuncPtr = fp;    Desc = desc;  }};

We can see that the functions implemented by the block and the access to variables are all implemented through the form parameters, and they are not members of the struct.

static void __KDBlockTest__test3_block_func_0(struct __KDBlockTest__test3_block_impl_0 *__cself, NSString *p1, NSString **p2, int p3, NSMutableArray *p4) {        p1=(NSString *)&__NSConstantStringImpl__var_folders_5l_2l25j3tn0wl_3zy1hpsq1rhc0000gp_T_KDBlockTest_654672_mi_9;        *p2=(NSString *)&__NSConstantStringImpl__var_folders_5l_2l25j3tn0wl_3zy1hpsq1rhc0000gp_T_KDBlockTest_654672_mi_10;        p3=23;        ((void (*)(id, SEL, id))(void *)objc_msgSend)((id)p4, sel_registerName("addObject:"), (id)(NSString *)&__NSConstantStringImpl__var_folders_5l_2l25j3tn0wl_3zy1hpsq1rhc0000gp_T_KDBlockTest_654672_mi_11);        NSLog((NSString *)&__NSConstantStringImpl__var_folders_5l_2l25j3tn0wl_3zy1hpsq1rhc0000gp_T_KDBlockTest_654672_mi_12,p1,p1,&p1);        NSLog((NSString *)&__NSConstantStringImpl__var_folders_5l_2l25j3tn0wl_3zy1hpsq1rhc0000gp_T_KDBlockTest_654672_mi_13,*p2,*p2,p2,&p2);        NSLog((NSString *)&__NSConstantStringImpl__var_folders_5l_2l25j3tn0wl_3zy1hpsq1rhc0000gp_T_KDBlockTest_654672_mi_14,p3,&p3);        NSLog((NSString *)&__NSConstantStringImpl__var_folders_5l_2l25j3tn0wl_3zy1hpsq1rhc0000gp_T_KDBlockTest_654672_mi_15,p4,p4,&p4);    }

The following is our test function. Looking at the red part, it is passed through function parameters. Like function calling, it is very different from using local variables, static variables, and member variables in the block, the variable is not intercepted.

Static void _ I _kdblocktest_test3 (kdblocktest * Self, Sel _ cmd) {nsstring * _ p1 = (nsstring * (*) (ID, Sel, nsstring *,...)) (void *) objc_msgsend) (ID) objc_getclass ("nsstring"), sel_registername ("stringwithformat:"), (nsstring *) & __ nsconstantstringimpl _ partition, (nsstring *) & __ nsconstantstringimpl _ separator); nsstring * _ P2 = (nsstring * (*) (ID, Sel, nsstring *,...)) (void *) objc_msgsend) (ID) objc_getclass ("nsstring"), sel_registername ("stringwithformat:"), (nsstring *) & __ nsconstantstringimpl _ partition, (nsstring *) & __ nsconstantstringimpl _ partition); int _ P3 = 1; nsmutablearray * _ P4 = (ID (*) (ID, Sel) (void *) objc_msgsend) (ID) (ID (*) (ID, Sel) (void *) objc_msgsend) (ID) objc_getclass ("nsmutablearray "), sel_registername ("alloc"), sel_registername ("init"); nslog (nsstring *) & __ nsconstantstringimpl _ partition, _ P1, _ P1, & _ P1 ); nslog (nsstring *) & __ nsconstantstringimpl _ partition, _ P2, _ P2, & _ P2); nslog (nsstring *) & __ nsconstantstringimpl _ partition, _ P3, & _ P3); nslog (nsstring *) & __ nsconstantstringimpl _ partition, _ P4, _ P4, & _ P4); void (* myblock) (nsstring *, nsstring **, Int, nsmutablearray *) = (void (*) (nsstring *, nsstring **, Int, nsmutablearray *) & __ kdblocktest _ test3_block_impl_0 (void *) _ kdblocktest _ test3_block_func_0, & __ kdblocktest _ blocks); (void (*) (_ block_impl *, nsstring *, nsstring **, Int, nsmutablearray *)) (_ block_impl *) myblock)-> funcptr) (_ block_impl *) myblock, _ P1, & _ P2, _ P3, _ P4 ); // The Block executes nslog (nsstring *) & __ nsconstantstringimpl _ delimiter, _ P1, _ P1, & _ P1); nslog (nsstring *) & __ nsconstantstringimpl _ partition, _ P2, _ P2, & _ P2); nslog (nsstring *) & __ nsconstantstringimpl _ partition, _ P3, & _ P3 ); nslog (nsstring *) & __ nsconstantstringimpl _ partition, _ P4, _ P4, & _ P4 );}

 

Summary:

  • For block parameters, the block is not strongly referenced by the form parameters and real parameters. The call is implemented by passing parameters (no member references are provided in the block structure)
  • Like a function call, a block can modify an internal parameter and affect the real parameter.

 

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.