respondsToSelector instancesRespondToSelector
2011-01-04 20:38
423 查看
要检查一个对象是否支持一个方法,我们可以使用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
除了检查对象是否支持一个特定的方法,我们还可以检查类是否会创建支持一个特定的方法的对象。未来做到这一点,我们使用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
相关文章推荐
- instancesRespondToSelector与respondsToSelector注意点
- respondsToSelector和 instancesRespondToSelector的理解
- respondsToSelector instancesRespondToSelector
- instancesrespondToSelector 是什么意思。与 respondsToSelector 有什么区别?
- Objective-C 动态类型(isKindOfClass, isMemberOfClass,respondsToSelector,instancesRespondToSelector
- isKindOfClass, isMemberOfClass, respondsToSelector, instancesRespondToSelector
- isKindOfClass& isMemberOfClass respondsToSelector& instancesRespondToSelector
- instancesRespondToSelector与respondsToSelector的区别
- instancesRespondToSelector与respondsToSelector的区别
- instancesRespondToSelector与respondsToSelector的区别
- instancesRespondToSelector与respondsToSelector的区别
- instancesRespondToSelector与respondsToSelector的区别
- 透彻讲解instancesRespondToSelector与respondsToSelector的区别
- instancesRespondToSelector与respondsToSelector的区别
- instancesRespondToSelector与respondsToSelector的区别
- instancesRespondToSelector与respondsToSelector的区别
- respondsToSelector的使用
- respondsToSelector的相关使用(非常好用的方法,一定要了解!!!)
- respondsToSelector判断是否实现了某方法
- Cannot use respondsToSelector using ARC on Mac