开发者

How do i invoke a class method by its name (method name as string)?

开发者 https://www.devze.com 2023-02-14 07:53 出处:网络
In my application,i came across a situtation,wheri开发者_Go百科n i need to invoke a particular class method by using method name (NSString).

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]
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号