使用-performSelector:与仅调用方法 [英] Using -performSelector: vs. just calling the method

查看:120
本文介绍了使用-performSelector:与仅调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然是Objective-C的新手,我想知道以下两个语句之间有什么区别?

I'm still kind of new to Objective-C and I'm wondering what is the difference between the following two statements?

[object performSelector:@selector(doSomething)]; 

[object doSomething];

推荐答案

基本上,performSelector允许您动态确定在给定对象上调用选择器的选择器.换句话说,不需要在运行时确定选择器.

Basically performSelector allows you to dynamically determine which selector to call a selector on the given object. In other words the selector need not be determined before runtime.

因此,即使这些是等效的:

Thus even though these are equivalent:

[anObject aMethod]; 
[anObject performSelector:@selector(aMethod)]; 

第二种形式允许您执行以下操作:

The second form allows you to do this:

SEL aSelector = findTheAppropriateSelectorForTheCurrentSituation();
[anObject performSelector: aSelector];

在发送消息之前.

这篇关于使用-performSelector:与仅调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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