Understanding uniqueness of selectors in Objective-C

前端 未结 4 1631
死守一世寂寞
死守一世寂寞 2021-01-15 03:50

I am having problem understanding part of the function of \"selectors\", as described in Apple\'s guide. I\'ve bolded the parts where I am getting confused:

4条回答
  •  既然无缘
    2021-01-15 04:20

    It also, though, refers to the unique identifier that replaces the name when the source code is compiled... All methods with the same name have the same selector.

    For this, I refer to an excellent blog post on selectors:

    A selector is the same for all methods that have the same name and parameters — regardless of which objects define them, whether those objects are related in the class hierarchy, or actually have nothing to do with each other. At runtime, Objective-C goes to the class and outright asks it, "Do you respond to this selector?", and calls the resulting function pointer if it does.

    The runtime system makes sure each identifier is unique: No two selectors are the same, and all methods with the same name have the same selector.

    In a screwed up way, this makes sense. If Method A and Method B have the exact same name and arguments, wouldn't it be more efficient to store their selector as one lookup and query the receiver instead of deciding between two basically equally named selectors at runtime?

提交回复
热议问题