菜鸟教程小白 发表于 2022-12-13 01:08:23

ios - 运行时 - 这个 "@@:"在 class_addMethod 中意味着什么?


                                            <p><p>使用<code>class_addMethod</code>代码:</p>

<pre><code>class_addMethod(newClass, @selector(inputAccessoryView), accessoryImp, &#34;@@:&#34;);
</code></pre>

<p>这个方法中参数“@@:”是什么意思?</p>

<p>文档:</p>

<pre><code>/**
* Adds a new method to a class with a given name and implementation.
*
* @param cls The class to which to add a method.
* @param name A selector that specifies the name of the method being added.
* @param imp A function which is the implementation of the new method. The function must take at least two arguments—self and _cmd.
* @param types An array of characters that describe the types of the arguments to the method.
*
* @return YES if the method was added successfully, otherwise NO
*(for example, the class already contains a method implementation with that name).
*
* @note class_addMethod will add an override of a superclass&#39;s implementation,
*but will not replace an existing implementation in this class.
*To change an existing implementation, use method_setImplementation.
*/
OBJC_EXPORT BOOL class_addMethod(Class cls, SEL name, IMP imp,
                           const char *types)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p><code>types</code> 参数描述了参数和返回类型
方法如中所述
<a href="https://developer.apple.com/reference/objectivec/1418901-class_addmethod?language=objc" rel="noreferrer noopener nofollow"><code>class_addMethod</code></a> :</p>

<blockquote>
<p>An array of characters that describe the types of the arguments to the method. For possible values, see <a href="https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html" rel="noreferrer noopener nofollow">Objective-C Runtime Programming Guide &gt; Type Encodings</a>. Since the function must take at least two arguments— <code>self</code> and <code>_cmd</code>, the second and third characters must be “@:” (the first character is the return type).</p>
</blockquote>

<p><code>"@@:"</code> 描述了一个返回对象的方法
(类型编码 <code>@</code>,在您的情况下:<code>UIView *</code>)并且除了固定(隐藏)参数 <code>self</code>(类型编码 <code>@</code> 用于对象)和 <code>_cmd</code>(类型编码 <code>:</code> 用于选择器)。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 运行时 - 这个&#34;@@:&#34;在 class_addMethod 中意味着什么?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/43492021/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/43492021/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 运行时 - 这个 &#34;@@:&#34;在 class_addMethod 中意味着什么?