`
fantom
  • 浏览: 141951 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

respondsToSelector

 
阅读更多
respondsToSelector该方法询问对象以确定其是否能够响应某个特定的消息

除了检查对象是否支持一个特定的方法,我们还可以检查类是否会创建支持一个特定的方法的对象。未来做到这一点,我们使用instancesRespondToSelector()方法
举例:

#import <Foundation/Foundation.h>

#import <stdio.h>
@interface Class1 : NSObject
{
}

-(void)print;
@end

@implementation Class1

-(void)print
{
printf("This is Class 1./n");

}

@end

int main (int argc, const char * argv[]) {
Class1 *c1=[Class1 new];

//验证对象支持一个方法

if ([c1 respondsToSelector: @selector(print)]==YES) {

printf("c1 has a print method. /n");

}

 

//验证类是否创建支持一个特定方法的对象

if ([Class1 instancesRespondToSelector: @selector(print)]==YES) {

printf("Class1 object have a print method /n/n");

}

    return 0;

}

输出结果:


c1 has a print method.

Class1 object have a print method
分享到:
评论

相关推荐

    ios demo,performSelector和respondsToSelector

    本示例“ios demo,performSelector和respondsToSelector”着重探讨了两个关键的编程概念:`performSelector`和`respondsToSelector`,它们是Objective-C运行时系统的重要组成部分,特别适用于动态添加方法和处理多...

    respondsToSelector与instancesRespondToSelector

    1、二者的作用相同:都是判断某个类是否实现了某...@2 类 + respondsToSelector + 类方法 yes = 类是否实现了某类方法 @3 实例 respondsToSelector 实例方法 yes = 类是否实现了某实例方法 且@1和@3的作用相同

    Obective-C委托

    5. **验证代理是否遵守协议**:在调用代理方法前,可以使用 `conformsToProtocol:` 或 `respondsToSelector:` 检查代理对象是否遵循协议或实现特定方法。例如: ```objc if ([myDelegate conformsToProtocol:@...

    OC简单实现代理

    if ([self.delegate respondsToSelector:@selector(myObjectWillStartTask:)]) { [self.delegate myObjectWillStartTask:self]; } // 执行任务 if ([self.delegate respondsToSelector:@selector...

    iOS 新闻系统源代码

    if ([self.delegate respondsToSelector:@selector(didSelectRow:andCityZip:)]){ CityItems *item = [_dataArray objectAtIndex:indexPath.row]; [self.delegate didSelectRow:self andCityZip:item.zip]; } ...

    Objective-C中消息转发机制demo

    - 如果对象的`- (BOOL) respondsToSelector:(SEL)aSelector`返回`NO`,但`- (BOOL) conformsToProtocol:(Protocol *)aProtocol`返回`YES`,表示对象遵循了包含该方法的协议,那么协议方法可以处理消息。 - 对象可以...

    iOS学习笔记3---delegate五步曲,轻松学会使用delegate

    if ([self.delegate respondsToSelector:@selector(toolBarDidRequestKeyboardToggle:)]) { [self.delegate toolBarDidRequestKeyboardToggle:self]; } } ``` 至此,我们完成了使用Delegate的基本流程。调用...

    OC非正式协议实现代理功能

    if ([self.delegate respondsToSelector:@selector(didFinishTask:)]) { [self.delegate didFinishTask:self]; } } @end ``` 4. **检查协议方法**:在调用代理方法前,我们需要检查代理对象是否实现了对应的方法...

    iPhone编程delegate示例

    if ([_delegate respondsToSelector:@selector(handleAction:)]) { [_delegate handleAction:info]; } } ``` 这里,`performAction:`方法被调用时,会通过`delegate`调用`handleAction:`,传递相关信息。 5. ...

    Runtime 实现NSCoding的自动归档和解档.zip

    if ([self respondsToSelector:decodeSelector]) { [self performSelector:decodeSelector withObject:coder]; } } } ``` 7. 最后,我们需要实现自定义的`encodeWithCoder:`和`initWithCoder:`,调用自动...

    iOS中正向、逆向传值的方法总结

    if ([_delegate respondsToSelector:@selector(twoViewController:sendValue:)]) { [_delegate twoViewController:self sendValue:_twoTextF.text]; } } ``` 在`OneViewController`中,遵守协议并设置代理: ``...

    ios 简单代理例程

    if ([self.delegate respondsToSelector:@selector(actionPerformed:)]) { [self.delegate actionPerformed:self]; } } - (void)didReceiveData:(NSData *)data { if ([self.delegate respondsToSelector:@...

    iOS开发教程:Storyboard全解析2

    if ([self.delegate respondsToSelector:@selector(playerDetailsViewControllerDidCancel:)]) { [self.delegate playerDetailsViewControllerDidCancel:self]; } } - (IBAction)done:(id)sender { if (...

    iOS11 WKWebView 无法加载内容的解决方法

    if ([self.realDelegate respondsToSelector:@selector(webView:decidePolicyForNavigationAction:decisionHandler:)]) { [self.realDelegate webView:webView decidePolicyForNavigationAction:navigationAction ...

    自定义键盘

    if ([self.delegate respondsToSelector:@selector(calculatorInputViewDidTapBackspace:)]) { [self.delegate calculatorInputViewDidTapBackspace:self]; } } /** 按键输入事件 */ - (void)userDidTapKey:...

    OC-performSelector

    因此,在使用`performSelector`前,最好先使用`instancesRespondToSelector:`或`respondsToSelector:`进行检查。 8. **KVC与KVO**: - `performSelector`在Key-Value Coding(KVC)和Key-Value Observing(KVO)中...

    iOS开发-动态库加载(实时模块更新).doc

    if ([dynamicOpenMenth respondsToSelector:@selector(startWithObject:withBundle:)]) { [dynamicOpenMenth startWithObject:self withBundle:bundle]; } } ``` 3. **测试动态库**: - 在主项目的某个事件...

    验证如何同时支持leftBarButtonItem及右滑返回手势_Objective-C_下载.zip

    if ([self.navigationController.interactivePopGestureRecognizer respondsToSelector:@selector(setEnabled:)]) { self.navigationController.interactivePopGestureRecognizer.enabled = YES; } ``` 3. **处理...

    IOS5 Programming Cookbook

    Based on the provided information from "iOS 5 Programming Cookbook" by Vandad Nahavandipoor, we can derive a comprehensive set of knowledge points related to iOS development using Objective-C....

    iOS大厂面试题集合

    if ([self respondsToSelector:@selector(accessInstanceVariablesDirectly)] && [self accessInstanceVariablesDirectly]) { // 直接访问实例变量 self._name = @"John"; } ``` - **注意事项**: - 直接访问...

Global site tag (gtag.js) - Google Analytics