gpt4 book ai didi

iphone - 使用 [self 方法] 或 @selector(方法)?

转载 作者:太空狗 更新时间:2023-10-30 03:25:54 25 4
gpt4 key购买 nike

谁能告诉我以下两个陈述之间的区别。

[self playButtonSound];

和:

[self performSelector:@selector(playButtonSound)];

我只是想问,因为我有一些使用 @selector 的旧代码,现在有了更多的知识,我想不出为什么我不使用 [self playButtonSound] 相反,它们似乎都与此处所写的相同。

加里

最佳答案

两者是一回事,但是 [self playButtonSound]; 绝对是在 Objective-C 中调用方法的正常方式。但是,使用 performSelector: 允许您调用仅在运行时确定的方法。

来自NSObject Protocol Reference :

The performSelector: method is equivalent to sending an aSelector message directly to the receiver. For example, all three of the following messages do the same thing:

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

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

关于iphone - 使用 [self 方法] 或 @selector(方法)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2674827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com