objective-c - iOS 中调用方法有什么区别

标签 objective-c

假设我调用

[self methodname] 

以及其他

[self performSelector:@selector(methodname) withObject:nil];

最佳答案

没有任何区别。

直接来自documentation performSelector:

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

尽管具体情况没有区别,但是 performSelector: 存在的原因是它允许调用在编译时可能不可用的任意选择器,如文档中所述:

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

上述注意事项也适用于两个变体 performSelector:withObject:performSelector:withObject:withObject:

另请注意,这不适用于另一组方法,即

  • performSelector:withObject:afterDelay:
  • performSelector:withObject:afterDelay:inModes:
  • performSelectorOnMainThread:withObject:waitUntilDone:
  • performSelectorOnMainThread:withObject:waitUntilDone:modes:
  • performSelector:onThread:withObject:waitUntilDone:
  • performSelector:onThread:withObject:waitUntilDone:modes:
  • performSelectorInBackground:withObject:

更多信息请点击:Does performSelector perform right away or is it scheduled to be performed?

关于objective-c - iOS 中调用方法有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19217433/

相关文章:

ios - 按“确定”按钮时选择当前日期和年份时发生崩溃

Objective-C imageWithCGImage 内存泄漏

objective-c - 在仅单例类的非初始化方法名称中使用 'init' 的 ARC 含义是什么

objective-c - 如何在运行时检查特定属性及其返回类型?

ios - -[CLLocation 长度] : unrecognized selector after table display

ios - 在 objective-c 中查找类的子类

ios - 如何在不显示中间 View 的情况下展开多个 View

objective-c - 使用多个 NSUInteger 枚举作为方法的参数

iphone - 如何将 UIImagePickerController 添加为 subview 而不是模态视图

ios - animateWithDuration 不是动画 UIButton