0

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

I've read Apple's documentation about selectors, but I still don't understand why there is a need for them.

As far as I can understand, selectors are equivalent to function pointers. Is that right?

Community
  • 1
  • 1
mskw
  • 10,063
  • 9
  • 42
  • 64
  • 1
    possible duplicate of [Are selectors in objective - c just another way to send a message to an object?](http://stackoverflow.com/questions/3543137/are-selectors-in-objective-c-just-another-way-to-send-a-message-to-an-object) and [Understanding selectors in ObjC](http://stackoverflow.com/questions/11051528/issue-understanding-a-part-of-selectors-of-objective-c) – jscs Jul 15 '12 at 21:04

2 Answers2

3

Selectors are used to obtain the method implementation of an object.

For example, almost every object implements the description method even if it inherits the implementation from its superclass. A function pointer can only point to one particular implementation of description (e.g. it can only point to NSString's version, or NSNumber's version, but not both). You can't take the function pointer of one class's description implementation and use it for every class, but you can use the same description selector, and it doesn't matter what object you use as the target, the right method implementation will always be used.

dreamlax
  • 93,976
  • 29
  • 161
  • 209
1

Selectors are not function pointers.

Selectors are unique-address method identifiers (message names). They're needed for the messenger function (objc_msgSend_*()) to be fast: when looking up a method name, the messenger doesn't need to do the rather expensive string comparison every time, because a selector has a unique address in memory, so a simple pointer comparison is enough.