respondsToSelector

-(BOOL) isKindOfClass: classObj 用来判断是否是某个类或其子类的实例
-(BOOL) isMemberOfClass: classObj 用来判断是否是某个类的实例
-(BOOL) respondsToSelector: selector 用来判断是否有以某个名字命名的方法(被封装在一个selector的对象里传递)
+(BOOL) instancesRespondToSelector: selector 用来判断实例是否有以某个名字命名的方法. 和上面一个不同之处在于, 前面这个方法可以用在实例和类上,而此方法只能用在类上.
-(id) performSelector: selector
SEL sel = @selector (start:) ; // 指定action
if ([obj respondsToSelector:sel])
{ //判断该对象是否有相应的方法
[obj performSelector:sel withObject:self]; //调用选择器方法
}
使用[[UIApplication sharedApplication] keyWindow]查找应用程序的主窗口对象

respondsToSelector方法对不同ios版本有类似动态支持的功能,例如在ios4.0以后中eagleView中新增了对 iphone4 Retina高清屏幕支持的成员变量scaleContent,在4.1sdk下可以直接使用self.scaleContent = 2,但是如果换用3.2sdk,这样编译马上就会报错,提示eagleView中没有这样的成员变量,这里报错也是自然,因为3.2中根本没有对高清的支持,这是出了四代的新硬件后新增的。那有没有办法使用同一份代码在不同的sdk版本中编译而又能动态运行在不同的ios中呢,这里,可以使用成员变量的get和set方法,而避免使用 “ . ”属性,再配合上respondsToSelector方法,这个问题就迎刃而解了。具体解决思路如下:
if([self respondsToSelector:@selector(setScaleContent:)])
{
[self setScaleContent:2]; //ios4.0以上才会执行进来
}
else
{
}

if和else语句的大括号中还可以加入一些自定义的代码,这样使用4.1sdk编译而目标设定为ios3.1.2的程序包就可以在不同的ios上有区别的动态执行代码了。
- (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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值