假设我打电话给
[self methodname]
和其他的
[self performSelector:@selector(methodname) withObject:nil];
假设我打电话给
[self methodname]
和其他的
[self performSelector:@selector(methodname) withObject:nil];
没有任何区别。
直接来自文档performSelector:
该
performSelector:
方法相当于aSelector
直接向接收者发送消息。例如,以下所有三个消息都执行相同的操作:id myClone = [anObject copy]; id myClone = [anObject performSelector:@selector(copy)]; id myClone = [anObject performSelector:sel_getUid("copy")];
尽管在特定情况下没有区别,但是存在的原因performSelector:
是它允许调用在编译时可能不可用的任意选择器,如文档中所述:
但是,该
performSelector:
方法允许您发送直到运行时才确定的消息。变量选择器可以作为参数传递: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:
此处的更多信息:performSelector 是立即执行还是计划执行?
[self methodname]` is shorter and easier to read, write and comprehend.
[self performSelector:@selector(methodname) withObject:nil]` makes it possible to execute arbitrary selectors. If you save the selector in a variable, then you can execute it later on without knowing the method you invoke.
//Self works like this in oops and self works as setter for your class. It also indicates that u r using getter and setter method.