Objective C 使用字符串动态调用方法 [英] Objective C calling method dynamically with a string

查看:26
本文介绍了Objective C 使用字符串动态调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道是否有一种方法可以调用我用字符串动态构建方法名称的方法.

Im just wondering whether there is a way to call a method where i build the name of the method on the fly with a string.

例如我有一个方法叫做 loaddata

e.g. I have a method called loaddata

-(void)loadData;

我通常会这样称呼它

[self loadData];

但我希望能够使用字符串动态调用它,例如

But i want to be able to call it dynamically with a string e.g.

NSString *methodName = [[NSString alloc] initWithString:@"loadData"];
[self methodName];

这是一个愚蠢的例子,但我希望你明白我的意思.我将它用于我为 iPad 应用程序设置的数据绑定类.很难解释,但要让它启动,我需要弄清楚如何使用字符串调用方法.

This is a stupid example but i hope you get my point. I am using it for databinding classes that I am setting up for my IPad application. Hard to explain but to get it to fire I need to work out how to call a method with a string.

有什么想法吗?

谢谢

推荐答案

你可以试试

SEL s = NSSelectorFromString(selectorName);
[anObject performSelector:s];

这篇关于Objective C 使用字符串动态调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆