LinkAddress:http://www.cnblogs.com/chenjunbiao/archive/2011/04/21/2023196.html
Tester.h
#import <Foundation/Foundation.h>
@interface Tester : NSObject {
}
-(void) test:(NSString*) msg;
-(void) notImp;
@end
@interface Tester : NSObject {
}
-(void) test:(NSString*) msg;
-(void) notImp;
@end
Tester.m
#import "Tester.h"
@implementation Tester
-(void) test:(NSString*) msg
{
NSLog(@"%@", msg);
}
@end
@implementation Tester
-(void) test:(NSString*) msg
{
NSLog(@"%@", msg);
}
@end
main.m
#import <Foundation/Foundation.h>
#import "Tester.h"
int main (int argc, constchar* argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
id tester = [[Tester alloc] init];//注意,这里使用id
SEL testSelector = @selector(test:);
SEL notImpSelector = @selector(notImp:);
if([tester respondsToSelector:testSelector])
{
//tester.m中实现了test方法
[tester test:@"invoke test method"];
}
if([tester respondsToSelector:notImpSelector])
{
//test.m中没有实现此主就去
[tester notImp];
}
[pool drain];
return0;
}
#import "Tester.h"
int main (int argc, constchar* argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
id tester = [[Tester alloc] init];//注意,这里使用id
SEL testSelector = @selector(test:);
SEL notImpSelector = @selector(notImp:);
if([tester respondsToSelector:testSelector])
{
//tester.m中实现了test方法
[tester test:@"invoke test method"];
}
if([tester respondsToSelector:notImpSelector])
{
//test.m中没有实现此主就去
[tester notImp];
}
[pool drain];
return0;
}
PS:
使用这种判断是比较保险的,尤其对于某一些委托函数,如果该委托没有实现在现实生活中却调用,那么该应用会崩溃掉。