How do i invoke a class method by its name (method name as string)?
In my application,i came across a situtation,wheri开发者_Go百科n i need to invoke a particular class method by using method name (NSString).
For ex:- there is class called test1 - having method hello.
@interface test1 : NSObject{
};
-hello:(id)vals;
@end
Now , i need to invoke/execute method hello() from here.
assume (id)instance is the instance of class test1.
-(void) RunFunction:(id)instance andFunctionName:(NSString*)fname andParamters:(id)params {
// need to do something like this - is it possible
// if yes how do i acheive this.
// sample java code starts.
Method m = instance.getClass().getDeclaredMethod(fname,params);
m.invoke(fname,params);
// sample java code ends.
}
could anyone help me in this.You would use -[NSObject performSelector:withObject:]
. In this case:
[instance performSelector:NSSelectorFromString(fname) withObject:params]
精彩评论