本文介绍了使用[self方法]或@selector(方法)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉我下面两个语句之间的区别。

Can anyone enlighten me as to the differences between the two statements below.

[self playButtonSound];

AND:

[self performSelector:@selector(playButtonSound)];

我只是问我,因为我有一些旧的代码使用 @selector ,现在有了更多的知识,我不能想到为什么我没有使用 [self playButtonSound] 相反,他们似乎都做同样的这里写的。

I am just asking as I had some old code that used @selector, now with a little more knowledge I can't think why I did not use [self playButtonSound] instead, they both seem to do the same as written here.

gary

推荐答案

但是 [self playButtonSound]; 绝对是调用Objective-C中的方法的正常方法。但是,使用 performSelector:可以调用只在运行时确定的方法。

Both to the same thing, but [self playButtonSound]; is definitely the normal way to invoke a method in Objective-C. However, using performSelector: allows you to call a method that is only determined at runtime.

href =http://developer.apple.com/mac/library/documentation/cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html#//apple_ref/occ/intfm/NSObject/performSelector: = nofollow noreferrer> NSObject协议参考:

From the NSObject Protocol Reference:

id myClone = [anObject copy];
id myClone = [anObject performSelector:@selector(copy)];
id myClone = [anObject performSelector:sel_getUid("copy")];

但是, performSelector: $ b允许您发送
在运行时之前未确定的消息。 A
变量选择器可以作为
参数传递:

However, the performSelector: method allows you to send messages that aren’t determined until runtime. A variable selector can be passed as the argument:

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


这篇关于使用[self方法]或@selector(方法)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 04:44