What's the difference between a method and a selector?

前端 未结 3 786
悲哀的现实
悲哀的现实 2020-11-30 16:41

What the difference between a method, a selector and a message in Objective-C?

3条回答
  •  鱼传尺愫
    2020-11-30 17:22

    A method is the implementation which is run when an object or class is asked to perform some action. It is in the scope of its containing class and is therefore different when referenced through some other class. A selector is an identifier which represents the name of a method. It is not related to any specific class or method, and can be used to describe a method of any class, whether it is a class or instance method.

    Simply, a selector is like a key in a dictionary. It can tell you what method someone is talking about, but only if you also have the dictionary itself (the class or object). The method is what you get when you ask for the value from the dictionary using the selector as a key.

提交回复
热议问题