假设我调用
[self methodname]
以及其他
[self performSelector:@selector(methodname) withObject:nil];
最佳答案
没有任何区别。
直接来自documentation performSelector:
The
performSelector:
method is equivalent to sending anaSelector
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/