Are selectors in Objective-C just another way to send a message to an object?

后端 未结 5 1000
误落风尘
误落风尘 2020-12-08 17:39

Are selectors in Objective-C just another way to send a message to an object? I really don\'t understand why or how to use them.

5条回答
  •  孤街浪徒
    2020-12-08 18:35

    --From Apple Developer Library --

    A selector is the name used to select a method to execute for an object, or the unique identifier that replaces the name when the source code is compiled. A selector by itself doesn’t do anything. It simply identifies a method. The only thing that makes the selector method name different from a plain string is that the compiler makes sure that selectors are unique. What makes a selector useful is that (in conjunction with the runtime) it acts like a dynamic function pointer that, for a given name, automatically points to the implementation of a method appropriate for whichever class it’s used with. Suppose you had a selector for the method run, and classes Dog, Athlete, and ComputerSimulation (each of which implemented a method run). The selector could be used with an instance of each of the classes to invoke its run method—even though the implementation might be different for each.

提交回复
热议问题