假设我打电话给
[self methodname]
和其他
[self performSelector:@selector(methodname) withObject:nil];
答案 0 :(得分:4)
没有任何区别。
直接来自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:
此处的详细信息:Does performSelector perform right away or is it scheduled to be performed?
答案 1 :(得分:1)
[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.