IMP获取函数指针

Tester.h

#import <Foundation/Foundation.h>


@interface Tester : NSObject {

}

-(void) Test:(NSString*) msg;

@end

 

Tester.m

#import "Tester.h"


@implementation Tester

-(void) Test:(NSString*) msg
{
NSLog(
@"%@", msg);

}


@end

main.m

#import <Foundation/Foundation.h>
#import
"Tester.h"

int main (int argc, const char * argv[])
{

NSAutoreleasePool
* pool = [[NSAutoreleasePool alloc] init];

id tester
= [[Tester alloc] init];

SEL testSelector
= @selector(Test:); //构造选择器
IMP testIMP = [tester methodForSelector: testSelector];//获取函数的指针

for(int i=0;i<10;i++)
{
//int to NSString
//直接使用指针调用对像函数
testIMP(tester,testSelector, [NSString stringWithFormat:@"%d",i]);
}


[pool drain];
return 0;
}

posted @ 2011-04-21 09:37  chenjunbiao  阅读(490)  评论(0编辑  收藏  举报