29,049
社区成员




@interface SelectorDemo : NSObject
-(void)InvokeSel:(SEL) select_test_method;
@end
@implementation SelectorDemo
-(void) InvokeSel:(SEL)select_test_method
{
if(select_test_method)
{
@try {
[self performSelector:select_test_method withObject:nil];
}
@catch (NSException *exception) {
printf("[Name]\n%s\n[Reson]\n%s\n\n",[exception.name UTF8String],[exception.reason UTF8String]);
}
}
}
#import <Foundation/Foundation.h>
#import "TRInvoker.h"
#import "TRSelTester.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
TRInvoker* invoker = [[TRInvoker alloc]init];
TRSelTester* tester = [[TRSelTester alloc]init];
tester.Invoker = invoker;
//测试1 - NSInvalidArgumentException
[tester InvokeSel:@selector(DisplaySentence)];
//测试2 - 成员方法调用
[invoker Invoke:tester];
}
return 0;
}
#import <Foundation/Foundation.h>
@interface TRSelTester : NSObject
@property (nonatomic,strong) id Invoker;
-(void)InvokeSel:(SEL) select_test_method;
@end
TRSelTester.m
#import "TRSelTester.h"
@interface TRSelTester ()
@end
@implementation TRSelTester
-(void)InvokeSel:(SEL) select_test_method
{
if(select_test_method)
{
@try {
[self.Invoker performSelector:select_test_method withObject:nil];
}
@catch (NSException *exception) {
printf("[Name]\n%s\n[Reson]\n%s\n\n",[exception.name UTF8String],[exception.reason UTF8String]);
}
}
}
@end
TRInvoker.h
#import <Foundation/Foundation.h>
#import "TRSelTester.h"
@interface TRInvoker : NSObject
-(void)DisplaySentence;
-(void)Invoke:(TRSelTester*) tester;
@end
TRInvoker.m
#import "TRInvoker.h"
@implementation TRInvoker
-(void)DisplaySentence
{
printf("display a sentence by manual.\n");
}
-(void)Invoke:(TRSelTester*) tester
{
[tester InvokeSel:@selector(DisplaySentence)];
}
@end
SelectorDemo *demo = [SelectorDemo new];
[demo InvokeSel:@selector(description)];
-(NSComparisonResult) compareNames:(id)element
{
return [((AddressCard*)element).name compare:self.name];
}
2.我这里是做练习,这个参数是SEL的例子,没做出来,比较不甘心,如果可以,能给一个简单例子,可以包含interface,implement和调用的么?有点小贪心啊,不过确实很期待。
喵。