What is use of performSelector in iOS

前端 未结 5 1132
不知归路
不知归路 2021-02-15 23:55

What is the role of performSelector?

Comparing:

[self btnClicked];

and

[self performSelector:@selector(btn         


        
5条回答
  •  温柔的废话
    2021-02-16 00:18

    Apple doc is your friend.

    NSObject Protocol Reference

    It

    Sends a specified message to the receiver and returns the result of the message.

    In particular:

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

    The aSelector argument should identify a method that takes no arguments. For methods that return anything other than an object, use NSInvocation.

    Hope that helps.

提交回复
热议问题