在目标C中“调用方法”或“发送消息” [英] 'Calling a method' OR 'sending a message' in Objective C

查看:241
本文介绍了在目标C中“调用方法”或“发送消息”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C或任何基于ECMAscript的语言中,您在对象上调用公共方法或函数。但是在Objective C的文档中,没有公共方法调用,只有消息的发送。

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 sending of messages.

当你'发送消息'时,是否有任何错误ObjC你实际上是'在对象上调用公共方法'。?

Is there anything wrong in thinking that when you 'send a message' in ObjC you are actually 'calling a public method on an Object'.?

推荐答案

从理论上讲,它们是不同的。

Theoretically, they're different.

实际上,不是那么多。

它们与Objective-C不同,对象可以选择不响应消息,或将消息转发到不同的对象,或其他什么。在像C这样的语言中,函数调用实际上只是跳转到内存中的某个位置并执行代码。没有涉及动态行为。

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.

但是,在标准用例中,当您向对象发送消息时,消息所代表的方法通常最终会被调用。因此,大约99%的时间,发送消息将导致调用方法。因此,当我们真正意味着发送消息时,我们经常说调用方法。所以实际上,它们几乎总是一样,但它们不一定是

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.

前一段时间,我对哲学有所了解这个主题和博客: http://davedelong.tumblr。 com / post / 58428190187 / an-observation-on-objective-c

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

编辑

要直接回答您的问题,说调用方法而不是发送消息通常没有错。但是,重要的是要理解存在非常显着的实现差异。

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")

这篇关于在目标C中“调用方法”或“发送消息”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆