菜鸟教程小白 发表于 2022-12-13 01:18:35

ios - 调用方法的无法识别的选择器


                                            <p><pre><code>- (NSDictionary*)convertMessage:(Message*)event
{
    // if this gets called then a derived class either didn&#39;t override this function or it called
    ;
    return nil;
}
</code></pre>

<p>我预计结果值为 nil。</p>

<pre><code>-(void)calling{

NSDictionary *dictionary= ;

}
</code></pre>

<p>但是它说无法识别的选择器发送到错误 block 的实例!运行时!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p><code></code>的实现是抛出异常。所以预期的结果是引发异常。如果您期望返回 nil,则只需在方法中返回 nil 并执行 <em>not</em> call <code>doesNotRecognizeSelector:</code></p>

<p>请参阅 <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html" rel="noreferrer noopener nofollow">apple&#39;s doc</a> </p>

<blockquote>
<p>The runtime system invokes this method whenever an object receives an
aSelector message it can’t respond to or forward. This method, in
turn, raises an NSInvalidArgumentException, and generates an error
message.</p>

<p>Any doesNotRecognizeSelector: messages are generally sent only by the
runtime system. However, they can be used in program code to prevent a
method from being inherited. For example, an NSObject subclass might
renounce the copy or init method by re-implementing it to include a
doesNotRecognizeSelector: message as follows:</p>
</blockquote>

<pre><code>- (id)copy {
    ;
}
</code></pre>

<blockquote>
<p>The _cmd variable is a hidden argument passed to every method that is the current selector;</p>

<p>in this example, it identifies the selector for the copy method. This
code prevents instances of the subclass from responding to copy
messages or superclasses from forwarding copy messages—although
respondsToSelector: will still report that the receiver has access to
a copy method.</p>

<p>If you override this method, you must call super or raise an
NSInvalidArgumentException exception at the end of your
implementation.
<strong>In other words, this method must not return normally;it must always result in an exception being thrown.</strong></p>
</blockquote></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 调用方法的无法识别的选择器,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/15085173/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/15085173/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 调用方法的无法识别的选择器