'Calling a method' OR 'sending a message' in Objective C

前端 未结 5 1168
旧巷少年郎
旧巷少年郎 2020-12-04 09:15

In C or any ECMAscript based language you \'call a public method or function\' on an object. But in documentation for Objective C, there are no public method calls, only the

5条回答
  •  日久生厌
    2020-12-04 09:55

    Theoretically, they're different.

    Practically, not so much.

    They're different in that in Objective-C, objects can choose to not respond to messages, or forward messages on to different objects, or whatever. In languages like C, function calls are really just jumping to a certain spot in memory and executing code. There's no dynamic behavior involved.

    However, in standard use cases, when you send a message to an object, the method that the message represented will usually end up being called. So about 99% of the time, sending a message will result in calling a method. As such, we often say "call a method" when we really mean "send a message". So practically, they're almost always the same, but they don't have to be.

    A while ago, I waxed philosophical on this topic and blogged about it: http://davedelong.tumblr.com/post/58428190187/an-observation-on-objective-c

    edit

    To directly answer your question, there's usually nothing wrong with saying "calling a method" instead of "sending a message". However, it's important to understand that there is a very significant implementation difference.

    (And as an aside, my personal preference is to say "invoke a method on an object")

提交回复
热议问题