po [self.delegate respondsToSelector:@selector(xxx)] nil

po [self.delegate respondsToSelector:@selector(xxx)] nil

一直没有发现的一个问题,今天在调试代码的时候发现,代理指针是有的,但是就是不执行代理方法。

检查来,检查去发现没有什么地方不对劲啊,但是奇怪的是有些代理方法可以正常回调,有些代理方法不被self.delegate响应,这是什么原因呢?

对比了以下可以响应和不能响应的方法发现,原来不执行代理方法回调,或者说po [self.delegate respondsToSelector:@selector(xxx)]的时候是nil,是因为xxx方法在签协议的类里没被实现。

一直以来,认为只要有代理指针,签了协议,self.delegate就会响应代理方法,即以下一定是会被执行。

[self.delegate respondsToSelector:@selector(xxx)]

今天才发现错了,也是挺nc的。对比输出看下结果

(lldb) po [self.delegate respondsToSelector:@selector(xxxx:xxxx)]
 nil
(lldb) po [self.delegate respondsToSelector:@selector(xxxx:xxxx)]
0x0000000000000001

想想也是理所当然的,如果签协议的类没有实现代理类的代理方法,po [self.delegate respondsToSelector:@selector(xxx)]为nil,或者说是不响应该方法,不就本来应该这样吗。想想也是很nc呢。

- (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
发出的红包

打赏作者

Morris_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值