响应方法
    响应方法

    [对象 performSelector:(SEL)];



#import <Foundation/Foundation.h>

//动物类
@interface Animal : NSObject
{
}
-(void)run;
@end

@implementation Animal
-(void)run{
NSLog(@"动物在跑!");
}
@end

//狗类
@interface Dog : NSObject
{
}
-(void)run;
-(void)eat;
-(void)eat:(NSString*)_foodName;
-(void)eat:(NSString*)_foodName andDogName :(NSString*)_dogName;
@end

@implementation Dog
-(void)run{
NSLog(@"狗在跑!");
}
-(void)eat{
NSLog(@"狗在吃");
}
-(void)eat:(NSString*)_foodName{
NSLog(@"狗在吃:%@", _foodName);
}
-(void)eat:(NSString*)_foodName andDogName :(NSString*)_dogName{
NSLog(@"这是%@狗,吃的食物是:%@",_dogName,_foodName);
}
@end

//以下是动态类型检测
int main(int argc,const char * argv[]){

Animal *animal = [Dog new];
SEL s1 = @selector(eat);
if([animal responseToSelector:s1]){
//这个performSelector就是响应方法 是无参的方法
[animal performSelector:s1];
}

SEL s2 = @selector(eat:);
if([animal respondsToSelector:s2]){
//这个是响应有参数的方法
//不能这么写,因为要传一个参数
//[animal performSelector:s2];

[animal performSelector:s2 withObject:@"有参数的响应方法"];
}

SEL s3 = @select(eat:andDogName:);
if([animal respondsToSelector:s3]){
//以下是响应一个方法,传递两个参数
[animal performSelector:s3 withObject:@"参数1" withObject:@"参数2"];
}

return 0;
}