IOS nsinvocation Application and understanding

Source: Internet
Author: User

There is a type in iOS that is SEL, it functions very similar to the function pointer, through the Performselector:withobject: function can call this message directly. But perform related to these functions, there is a limitation, the number of parameters can not exceed 2, or to do a very cumbersome processing, in contrast, nsinvocation is also a method of message invocation, and its parameters are not limited. These two methods of directly invoking object messages, after IOS4.0, are mostly superseded by the block structure, and are only used in very old compatibility systems, as summarized in simple usage:

First, initialize and invoke

It is clearly stated in the official documentation that the Nsinvocation object can only be initialized with its class method, and the Alloc/init method cannot be used. Before it executes the call, it needs to set two methods: Setselector: And Setargument:atindex:

-  (void) viewdidload {     [super viewDidLoad];    SEL myMethod =  @selector (MyLog);     //creates a function signature, which can be arbitrary, but it is important to note that the number of parameters of the signature function is consistent with the invocation.     NSMethodSignature * sig  = [NSNumber  Instancemethodsignatureforselector: @selector (init)];    //initialized by signature      nsinvocation * invocatin = [nsinvocation invocationwithmethodsignature:sig];     //setting target    [invocatin settarget:self];    // Set selecteor    [invocatin setselector:mymethod];    //message Call      [invocatin invoke];    }-(void) Mylog{    nslog ( @ "MyLog");} 

Note: The number of parameters for the signature function is consistent with the calling function. After testing, it was found that there was no problem when the number of signature function parameters was greater than the function.

Call the Multi-parameter method, we can write this:

-  (void) Viewdidload {    [super viewdidload];    sel  myMethod =  @selector (myLog:parm:parm:);    nsmethodsignature *  sig  = [[self class] instancemethodsignatureforselector:mymethod];     NSInvocation * invocatin = [NSInvocation invocationWithMethodSignature:sig];     [invocatin settarget:self];    [invocatin setselector: mymethod2];    int a=1;    int b=2;     int c=3;    [invocatin setargument:&a atindex:2];     [invocatin setargument:&b atindex:3];    [invocatin setargument: &c atindex:4];    [invocatin invoke];} -(void) MyLog: (int) A parm: (int) B parm: (int) C{ &nbsP;  nslog (@ "mylog%d:%d:%d", A,b,c);} 

Note: 1, the index of the set parameter here needs to start from 2, because the first two are occupied by selector and target. There is no problem with writing this:

-  (void) Viewdidload {    [super viewdidload];    sel  myMethod =  @selector (myLog:parm:parm:);    sel mymethod2 =  @selector (MYLOG);     nsmethodsignature * sig  = [[self class ] instancemethodsignatureforselector:mymethod];    nsinvocation * invocatin  = [nsinvocation invocationwithmethodsignature:sig];    viewcontroller  * view = self;    [invocatin setargument:&view atindex :0];    [invocatin setargument:&mymethod2 atindex:1];     int a=1;    int b=2;    int c=3;     [invocatin setArgument:&a atIndex:2];    [invocatin  setargument:&b atindex:3];&Nbsp;   [invocatin setargument:&c atindex:4];    [invocatin  retainarguments];    [invocatin invoke];} -(void) MyLog: (int) A parm: (int) B parm: (int) C{    nslog (@ "mylog%d:%d:%d", A,b,c);}

2, the method must be passed the parameter address.

Second, the return value of Nsinvocation

The Nsinvocation object, which can have a return value, but this return value, is not the return value of the function it calls, we need to set it manually:

-  (void) Viewdidload {    [super viewdidload];    sel  myMethod =  @selector (myLog:parm:parm:);    nsmethodsignature *  sig  = [[self class] instancemethodsignatureforselector:mymethod];     NSInvocation * invocatin = [NSInvocation invocationWithMethodSignature:sig];     [invocatin settarget:self];    [invocatin setselector: mymethod2];    viewcontroller * view = self;      int a=1;    int b=2;    int c=3;     [invocatin setArgument:&view atIndex:0];    [invocatin  setargument:&mymethod2 atindex:1];    [invocatin setargument:&a  atindex:2];     [Invocatin setargument:&b atindex:3];    [invocatin setargument:&c  atindex:4];    [invocatin retainarguments];    // We set the value of C to the return value     [invocatin setReturnValue:&c];    int d;     //take this return value     [invocatin getReturnValue:&d];     nslog (@ "%d", d);     }-(int) MyLog: (int) A parm: (int) B parm: (int) c{     nslog (@ "mylog%d:%d:%d", a,b,c);     return a+b+c;}

Note: The action here is to pass the address. If it is an OC object, the address is also taken.

Third, about memory

It can be noted that-(void) retainarguments; This method, which will retain all parameters passed in and target all over again.

IOS nsinvocation Application and understanding

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.