Objective-C RespondsToSelector

本文介绍了Objective-C RespondsToSelector的处理方法,对大家解决问题具有一定的参考价值

问题描述

到目前为止我所学到的:在Objective-C中,您可以向任何对象发送任何消息.如果对象确实实现了正确的方法,它将被执行,否则什么都不会发生.这是因为在发送消息之前,Objective-C 会执行 respondsToSelector.

我希望到目前为止我是对的.

我做了一个小程序来测试每次移动滑块时调用的动作.同样为了测试,我将发送者设置为 NSButton 但实际上它是一个 NSSlider.现在我询问对象是否会响应 setAlternateTitle.虽然 NSButton 会做而 NSSlider 不会.如果我运行代码并自己执行 respondsToSelector ,它会告诉我对象不会响应该选择器.如果我测试其他类似 intValue 的东西,它会响应.所以到目前为止我的代码很好.

- (IBAction)sliderDidMove:(id)sender{NSButton *slider = 发件人;BOOL 响应 =[slider RespondsToSelector:@selector(setAlternateTitle)];如果(响应 == 是){NSLog(@"YES");}别的{NSLog(@"NO");}[slider setAlternateTitle:@"Hello World"];}

但是当我真正发送 setAlternateTitle 消息时,程序会崩溃,我不确定为什么.发送消息之前不应该做一个respondsToSelector吗?

解决方案

首先,方法的名称(其选择器)包括所有子部分和冒号字符,正如 mvds 所说.

其次,方法 -respondsToSelector: 不是由运行时调用的,它通常由用户调用(例如,您自己或想要知道委托是否响应的 API)协议的可选方法).

当你向一个对象发送消息时,运行时会在该对象的类中寻找该方法的实现(通过对象的isa指针).它等同于发送 -respondsToSelector: 尽管消息本身没有被调度.如果在类或其超类中找到该方法的实现,则会使用您传入的所有参数调用它.

如果不是,那么运行时会给消息第二次执行的机会.它将首先发送消息 + (BOOL)resolveInstanceMethod:(SEL)name 到对象的类:此方法允许您在运行时将方法添加到类中:如果此消息返回 YES,这意味着它可以重新发送消息.

如果不是它给消息第三次执行机会,它发送-(id)forwardingTargetForSelector:(SEL)aSelector和选择器,这个方法可以返回另一个可能能够执行的对象代表实际接收者响应选择器,如果返回的对象可以响应,则执行该方法并返回值,就好像它是由原始消息返回的一样.(注意:此功能从 OS X 10.6 或 iOS 4 开始可用.)

如果返回的对象是 nil 或 self(为了避免无限循环),运行时会给消息第四次执行方法的机会......它发送消息 - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector 获取方法签名以构建调用.如果提供了一个调用,则通过消息 - (void)forwardInvocation:(NSInvocation *)anInvocation 发送调用.在这个方法中,你可以解析调用并构建其他消息以任何你想要的方式发送到其他目标,然后你可以设置调用的返回值......该值将作为原始消息的返回值.

最后,如果对象没有返回方法签名,那么运行时会发送消息- (void)doesNotRecognizeSelector:(SEL)aSelector 给你的对象,NSObject类中这个方法的实现抛出异常.

From what I have learned so far: In Objective-C you can send any message to any object. If the object does implement the right method it will be executed otherwise nothing will happen. This is because before the message is sent Objective-C will perform respondsToSelector.

I hope I am right so far.

I did a little program for testing where an action is invoked every time a slider is moved. Also for testing I set the sender to NSButton but in fact it is an NSSlider. Now I asked the object if it will respond to setAlternateTitle. While a NSButton will do and NSSlider will not. If I run the code and do respondsToSelector myself it will tell me the object will not respond to that selector. If I test something else like intValue, it will respond. So my code is fine so far.

- (IBAction)sliderDidMove:(id)sender
{
    NSButton *slider = sender;

    BOOL responds =
    [slider respondsToSelector:@selector(setAlternateTitle)];

    if(responds == YES)
    {
        NSLog(@"YES");        
    }
    else
    {
        NSLog(@"NO");
    }

    [slider setAlternateTitle:@"Hello World"];
}

But when I actually send the setAlternateTitle message the program will crash and I am not exactly sure why. Shouldn't it do a respondsToSelector before sending the message?

解决方案

First of all, the name of a method (its selector) includes all subparts and colon characters, as mvds said.

Second of all, the method -respondsToSelector: is not called by the runtime, it's usually called by the user (yourself or APIs that want to know if a delegate, for example, responds to an optional method of the protocol).

When you send a message to an object, the runtime will look for the implementation of the method in the class of the object (through the object's isa pointer). It's equivalent to sending -respondsToSelector: although the message itself is not dispatched. If the implementation of the method is found in the class or in its superclasses, it's called with all the arguments you passed in.

If not, then the runtime gives the message a second chance to be executed. It will start by sending the message + (BOOL)resolveInstanceMethod:(SEL)name to the class of the object: this method allows you to add the method at runtime to the class: if this message returns YES, it means it can redispatch the message.

If not it gives the message a third chance to be executed, it sends - (id)forwardingTargetForSelector:(SEL)aSelector with the selector, this method can return another object that may be able to respond to the selector on behalf of the actual receiver, if the returned object can respond, the method is executed and the value is returned as if it was returned by the original message. (Note: This is available beginning with OS X 10.6 or iOS 4.)

If the returned object is nil or self (to avoid infinite loops), the runtime gives the message a fourth chance to execute the method… It sends the message - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector to get a method signature in order to build an invocation. If one is provided then an invocation is sent through the message - (void)forwardInvocation:(NSInvocation *)anInvocation. In this method you can parse the invocation and build other messages to send to other targets in any ways you want, and then you can set the return value of the invocation… That value will act as the return value of the original message.

Finally, if no method signature is returned by the object, then the runtime sends the message - (void)doesNotRecognizeSelector:(SEL)aSelector to your object, the implementation of this method in NSObject class throws an exception.

这篇关于Objective-C RespondsToSelector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2

admin_action_{$_REQUEST[‘action’]}

do_action( "admin_action_{$_REQUEST[‘action’]}" )动作钩子::在发送“Action”请求变量时激发。Action Hook: Fires when an ‘action’ request variable is sent.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$_REQUEST['action']引用从GET或POST请求派生的操作。源码(Source)更新版本源码位置使用被使用2.6.0 wp-admin/admin.php:...

日期:2020-09-02 17:44:16 浏览:1170

admin_footer-{$GLOBALS[‘hook_suffix’]}

do_action( "admin_footer-{$GLOBALS[‘hook_suffix’]}", string $hook_suffix )操作挂钩:在默认页脚脚本之后打印脚本或数据。Action Hook: Print scripts or data after the default footer scripts.目录锚点:#说明#参数#源码说明(Description)钩子名的动态部分,$GLOBALS['hook_suffix']引用当前页的全局钩子后缀。参数(Parameters)参数类...

日期:2020-09-02 17:44:20 浏览:1071

customize_save_{$this->id_data[‘base’]}

do_action( "customize_save_{$this->id_data[‘base’]}", WP_Customize_Setting $this )动作钩子::在调用WP_Customize_Setting::save()方法时激发。Action Hook: Fires when the WP_Customize_Setting::save() method is called.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_data...

日期:2020-08-15 15:47:24 浏览:807

customize_value_{$this->id_data[‘base’]}

apply_filters( "customize_value_{$this->id_data[‘base’]}", mixed $default )过滤器::过滤未作为主题模式或选项处理的自定义设置值。Filter Hook: Filter a Customize setting value not handled as a theme_mod or option.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_date['base'],指的是设置...

日期:2020-08-15 15:47:24 浏览:900

get_comment_author_url

过滤钩子:过滤评论作者的URL。Filter Hook: Filters the comment author’s URL.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/comment-template.php:32610...

日期:2020-08-10 23:06:14 浏览:930

network_admin_edit_{$_GET[‘action’]}

do_action( "network_admin_edit_{$_GET[‘action’]}" )操作挂钩:启动请求的处理程序操作。Action Hook: Fires the requested handler action.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$u GET['action']引用请求的操作的名称。源码(Source)更新版本源码位置使用被使用3.1.0 wp-admin/network/edit.php:3600...

日期:2020-08-02 09:56:09 浏览:877

network_sites_updated_message_{$_GET[‘updated’]}

apply_filters( "network_sites_updated_message_{$_GET[‘updated’]}", string $msg )筛选器挂钩:在网络管理中筛选特定的非默认站点更新消息。Filter Hook: Filters a specific, non-default site-updated message in the Network admin.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分$_GET['updated']引用了非默认的...

日期:2020-08-02 09:56:03 浏览:864

pre_wp_is_site_initialized

过滤器::过滤在访问数据库之前是否初始化站点的检查。Filter Hook: Filters the check for whether a site is initialized before the database is accessed.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/ms-site.php:93910...

日期:2020-07-29 10:15:38 浏览:834

WordPress 的SEO 教学:如何在网站中加入关键字(Meta Keywords)与Meta 描述(Meta Description)?

你想在WordPress 中添加关键字和meta 描述吗?关键字和meta 描述使你能够提高网站的SEO。在本文中,我们将向你展示如何在WordPress 中正确添加关键字和meta 描述。为什么要在WordPress 中添加关键字和Meta 描述?关键字和说明让搜寻引擎更了解您的帖子和页面的内容。关键词是人们寻找您发布的内容时,可能会搜索的重要词语或片语。而Meta Description则是对你的页面和文章的简要描述。如果你想要了解更多关于中继标签的资讯,可以参考Google的说明。Meta 关键字和描...

日期:2020-10-03 21:18:25 浏览:1727

谷歌的SEO是什么

SEO (Search Engine Optimization)中文是搜寻引擎最佳化,意思近于「关键字自然排序」、「网站排名优化」。简言之,SEO是以搜索引擎(如Google、Bing)为曝光媒体的行销手法。例如搜寻「wordpress教学」,会看到本站的「WordPress教学:12个课程…」排行Google第一:关键字:wordpress教学、wordpress课程…若搜寻「网站架设」,则会看到另一个网页排名第1:关键字:网站架设、架站…以上两个网页,每月从搜寻引擎导入自然流量,达2万4千:每月「有机搜...

日期:2020-10-30 17:23:57 浏览:1308