iOS development--practical technology OC Chapter &? Invocation Brief Introduction

Source: Internet
Author: User
<span id="Label3"></p><p style="text-align: center;"><p style="text-align: center;"><strong><span style="font-size: 18pt;">Invocation Brief Introduction</span><br></strong></p></p><p><p><strong><br><span style="font-size: 18px; color: #0000ff;">method one: run-time method:</span>(here in the previous article timer of several methods said: WWW.CNBLOGS.COM/ICOCOS/P/4694581.HTML)<br></strong></p></p><p><p></p></p><p><p>1: Create a Signature:</p></p><pre><pre>Nsmethodsignature *singature = [nsmethodsignature signaturewithobjctypes:<span style="color: #800000;">"</span><span style="color: #800000;">[email protected]:</span><span style="color: #800000;">" </span></pre></pre><p><p>Here I think if you are careful, you must have noticed: the following "[email protected]:", here is the syntax of the runtime<br>In this case, it means a method.<br>V Put back Viod type<br>@ An object of type ID<br>•: corresponding SEL<br>About the runtime not much here. please check the Author's previous article, or view the official documentation, which is a knowledge point that iOS developers must have<br>2: create a request by the front and set the corresponding target and SEL</p></p><pre><pre> Nsinvocation *vocation =<span style="color: #000000;"> [nsinvocation invocationwithmethodsignature:singature]; </span> = self<span style="color: #000000;"> ; </span> = @selector (timechange);</pre></pre><p><p><br>3: in the request to the nstimer to achieve the timing</p></p><pre><pre>[nstimer scheduledtimerwithtimeinterval:<span style="color: #800080;">1</span></pre></pre><p><p><strong><br><span style="color: #0000ff; font-size: 18px;">Method Two: directly create the corresponding class</span><br><br>Signature Signature: When creating a nsinvocation, a Signature object must be passed<br>The role of the signature object: to get the number of arguments and the return value of the method<br>Note: when you create a signature object, you do not use the Nsmethodsignature class to create \</strong></p></p><p><p><strong><br>One: It's the way it belongs to who creates it.<br></strong></p></p><pre class="brush:csharp;gutter:true;"><pre class="brush:csharp;gutter:true;"> Nsmethodsignature *signature = [viewcontroller instancemethodsignatureforselector: @selector (sendMessageWithNumber: andcontent:status:)];</pre></pre><p><p><strong>nsinvocation; Used to wrap the method and the corresponding object, it can store the name of the method, corresponding object, corresponding parameter<br>Two:. Create a Nsinvocation object</strong></p></p><pre><pre> Nsinvocation *invocation = [nsinvocation invocationwithmethodsignature:signature];</pre></pre><p><p><strong><br>Three: set the corresponding property<br></strong></p></p><pre><pre> <span style="color: #008000;">//</span> <span style="color: #008000;">the object to which the Save method belongs</span> <span style="color: #008000;">//</span> the <span style="color: #008000;">method to be set for invocation must be consistent with the method in the signature</span> <span style="color: #008000;">//</span> <span style="color: #008000;">Save Method Name</span></pre></pre><p><p><strong>First parameter: the value that needs to be passed to the specified method<br>+ The first parameter needs to receive a pointer, that is, pass the address when passing the value<br>Second Parameter: the value of the first parameter of the specified method needs to be passed<br>Four: implementation of parameter settings<br></strong></p></p><pre><pre><span style="color: #800000;">@"</span> <span style="color: #800000;">Icocos</span> <span style="color: #800000;">"</span>;</pre></pre><p><p><strong><br>Note: You cannot set the index of a parameter to start at 0 because 0 is already occupied by self and 1 has been _cmd occupied<br></strong></p></p><pre><pre> [invocation setargument:&number atindex:<span style="color: #800080;">2</span>];</pre></pre><p><p><strong>2. Invoking the Invoke method of the Nsinvocation object<br>As long as the Invoke method of invocation is called, it represents the need to execute \<br>V: Specifies the specified method of the object in the Nsinvocation object, and passes the specified argument<br></strong></p></p><pre><pre> [invocation invoke];</pre></pre><p><p><strong><br><span style="color: #ff6600; font-size: 14pt;">Nsinvocation Classic case: Encapsulation of Multi-parameter methods:</span><br></strong></p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">ID</span></span>) performselector: (SEL) aselector withobjects: (nsarray *<span style="color: #000000;"><span style="color: #000000;">) Objects</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #000000;"><span style="color: #000000;">{</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">1. Create a Signature object</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span>Nsmethodsignature *signature = [self<span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">] instancemethodsignatureforselector:aselector];</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">determines whether the incoming method is stored, if the method does not exist, the Signature object is nil</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(signature = =<span style="color: #000000;"><span style="color: #000000;">Nil) {</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">the method passed in does not exist</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span>NSString *info = [nsstring stringwithformat:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">-[%@%@]: Unrecognized selector sent to instance</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>, [self<span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">], nsstringfromselector (aselector)];</span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span> <span style="color: #008080;"><span style="color: #008080;"></span> one</span> <span style="color: #0000ff;"><span style="color: #0000ff;">@throw</span></span>[[nsexception alloc] initwithname:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">the mistake of a cow b</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">Reason:info userinfo:nil];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">2. Create a Nsinvocation object</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>Nsinvocation *invocation =<span style="color: #000000;"><span style="color: #000000;">[nsinvocation invocationwithmethodsignature:signature];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">3. Save the object to which the method belongs</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Invocation.target =<span style="color: #000000;">self <span style="color: #000000;">;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">the <span style="color: #008000;">method to be set for invocation must be consistent with the method in the signature</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">4. Save Method name</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span>Invocation.selector =<span style="color: #000000;"><span style="color: #000000;">aselector;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> at</span> <span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">5. Setting Parameters</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">There is a problem if you are currently iterating through a parameter array to set parameters</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">if the parameter array element is redundant, then an error will be</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #008080;"><span style="color: #008080;"></span> in</span>Nsuinteger arguments = signature.numberofarguments-<span style="color: #800080;"><span style="color: #800080;">2</span></span><span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008080;"><span style="color: #008080;"></span> to</span> <span style="color: #008000;"><span style="color: #008000;">If you traverse the number of parameter values directly, There is a problem</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #008000;"><span style="color: #008000;">if the number of arguments is greater than the number of parameter values, the array will be out of bounds</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>Nsuinteger Objectscount =<span style="color: #000000;"><span style="color: #000000;">objects.count;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> *</span> <span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008080;"><span style="color: #008080;"></span> $</span> <span style="color: #008000;"><span style="color: #008000;">parameters and parameter values, who are less traversed by who</span></span><span style="color: #008080;"><span style="color: #008080;">Panax Notoginseng</span></span> <span style="color: #008000;"><span style="color: #008000;">*/</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Nsuinteger count =<span style="color: #000000;"><span style="color: #000000;">MIN (arguments, objectscount);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>i =<span style="color: #800080;"><span style="color: #800080;">0</span></span>; I < count; i++<span style="color: #000000;"><span style="color: #000000;">) {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span>NSObject *obj =<span style="color: #000000;"><span style="color: #000000;">objects[i];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">dealing with nsnull problems in array parameters</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>([obj Iskindofclass:[nsnull<span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">]]) {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>obj =<span style="color: #000000;"><span style="color: #000000;">nil;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> $</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> $</span>[invocation Setargument:&obj atindex:i +<span style="color: #800080;"><span style="color: #800080;">2</span></span><span style="color: #000000;"><span style="color: #000000;">];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">6. Invoking the Invoke method of the Nsinvocation object</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">[invocation invoke];</span></span><span style="color: #008080;"><span style="color: #008080;">Wuyi</span></span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #0000ff;"><span style="color: #0000ff;">ID</span></span>res =<span style="color: #000000;"><span style="color: #000000;">nil;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">determines whether the current method has a return value</span></span><span style="color: #008080;"><span style="color: #008080;"></span> wu</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">NSLog (@ "returntype =%zd", signature.methodreturnlength);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(signature.methodreturnlength! =<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">) {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> about</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">7. Get the return value</span></span><span style="color: #008080;"><span style="color: #008080;"></span> $</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">the <span style="color: #008000;">Getreturnvalue method assigns the returned value of the method to the incoming object</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>[invocation getreturnvalue:&<span style="color: #000000;"><span style="color: #000000;">res];</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">res;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span>}</pre><p><p></p></p><p><p><strong><br><br>After we have more parameters do not have to worry about, and achieve a variety of optimizations, in the previous we use JS call OC when it is not so troublesome, directly passed a parameter (array) in the parameters and corresponding methods can be implemented to achieve the corresponding function</strong></p></p><p><p>iOS development--practical technology OC Chapter &? Invocation Brief Introduction</p></p></span>

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.