instancesRespondToSelector:和respondsToSelector:

官方介绍如下
不能通过super关键字向对象发送respondsToSelector:来测试对象是否从其超类继承方法。这个方法不仅仅对对象的超类的实现测试,也会测试整个对象的类的实现。因此,向super发送respondsToSelector:相当于将其发送给自己。

如果想测试超类是否实现了某个方法, 必须直接在对象的超类上调用NSObject的类方法instancesRespondToSelector:,如以下代码片段所示。

if([MySuperclass instancesRespondToSelector:@selector(aMethod)]){
    //调用继承的方法
    [super aMethod];
}

你不能简单地使用[[self superclass] instancesRespondToSelector:@selector(aMethod)],因为如果它被子类调用,这可能会导致方法失败。
注意,如果接收器能够将aSelector消息转发到另一个对象,而且被转发的对象也能够响应该消息,尽管是间接的,那么这个方法也将返回NO。

看到某个博客上的总结挺好,抄录如下
https://www.jianshu.com/p/bf8422d53b88

  • instancesRespondToSelector 为类方法,respondsToSelector 是协议方法,类和实例都可调用。
  • 类调用 respondsToSelector 用于判断是否包含某个类方法,实例调用 respondsToSelector - 用于判断是否包含某个实例方法。
  • 类调用 instancesRespondToSelector 用于判断该类的实例是否包含某方法,等效于该类的实例调用 respondsToSelector。
  • 在一个类中不可以通过 [super respondsToSelector:…]; 的方式判断父类中是否实现或继承了某个方法,因为这个方法会检测整个类,和 [self respondsToSelector:…]; 等价。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
- (void)close { // Empty queues. [self emptyQueues]; [partialReadBuffer release]; partialReadBuffer = nil; [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(disconnect) object:nil]; // Close streams. if (theReadStream != NULL) { CFReadStreamSetClient(theReadStream, kCFStreamEventNone, NULL, NULL); CFReadStreamUnscheduleFromRunLoop (theReadStream, theRunLoop, kCFRunLoopDefaultMode); CFReadStreamClose (theReadStream); CFRelease (theReadStream); theReadStream = NULL; } if (theWriteStream != NULL) { CFWriteStreamSetClient(theWriteStream, kCFStreamEventNone, NULL, NULL); CFWriteStreamUnscheduleFromRunLoop (theWriteStream, theRunLoop, kCFRunLoopDefaultMode); CFWriteStreamClose (theWriteStream); CFRelease (theWriteStream); theWriteStream = NULL; } // Close sockets. if (theSocket != NULL) { CFSocketInvalidate (theSocket); CFRelease (theSocket); theSocket = NULL; } if (theSocket6 != NULL) { CFSocketInvalidate (theSocket6); CFRelease (theSocket6); theSocket6 = NULL; } if (theSource != NULL) { CFRunLoopRemoveSource (theRunLoop, theSource, kCFRunLoopDefaultMode); CFRelease (theSource); theSource = NULL; } if (theSource6 != NULL) { CFRunLoopRemoveSource (theRunLoop, theSource6, kCFRunLoopDefaultMode); CFRelease (theSource6); theSource6 = NULL; } theRunLoop = NULL; // If the client has passed the connect/accept method, then the connection has at least begun. // Notify delegate that it is now ending. if (theFlags & kDidPassConnectMethod) { // Delay notification to give him freedom to release without returning here and core-dumping. if ([theDelegate respondsToSelector: @selector(onSocketDidDisconnect:)]) { //[theDelegate performSelector:@selector(onSocketDidDisconnect:) withObject:self afterDelay:0]; [theDelegate onSocketDidDisconnect:self]; } } // Clear flags. theFlags = 0x00; }
最新发布
06-13

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员的修养

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值