C 函数与 Objective-C 方法?

发布于 2024-10-14 18:58:03 字数 198 浏览 2 评论 0原文

两者有什么区别?如果我正在编写一个程序,我什么时候需要 this:

void aFunction() {
    //do something
}

以及什么时候需要 this:

-(void)aMethod {
    //do something else
}

What is the difference between the two? If I'm writing a program, when would I need a this:

void aFunction() {
    //do something
}

and when would I need this:

-(void)aMethod {
    //do something else
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挽容 2024-10-21 18:58:03

实际上,Objective-C 方法只是一个始终在开头出现两个参数的 C 函数。

这:

-(void)aMethod;

完全等价于:

void function(id self, SEL _cmd);

Objective-C 的消息传递是这样的:

[someObject aMethod];

完全等价于这个(几乎 - 有一个可变参数 ABI 问题超出了这个答案的范围):

objc_msgSend(someObject, @selector(aMethod));

objc_msgSend() 找到适当的实现方法(通过在 someObject 上查找),然后通过尾部调用优化的魔力,跳转到该方法的实现,无论出于何种目的,该方法的工作方式与 C 函数调用完全相同看起来像这样:

function(someObject, @selector(aMethod));

从字面上看,Objective-C 最初只是作为一个 C 预处理器来实现的。 任何你在 Objective-C 中能做的事情都可以被重写为直接的 C。

然而,这样做会是一件非常痛苦的事情,而且除了令人难以置信的教育体验之外,不值得你花时间。


一般来说,在与对象对话时使用 Objective-C 方法,在使用直接 C 语言时使用函数。鉴于几乎所有 Mac OS X 和 iOS 都提供 Objective-C API(对于 UI 级编程入口点当然完全如此),那么您大部分时间都使用 Obj-C。

即使在编写自己相对独立的模型级代码时,您通常也会使用 Objective-C,因为它在状态/数据和状态/数据之间提供了非常自然的粘合。功能,面向对象编程的基本租户。

Actually, an Objective-C method is just a C function with two arguments always present at the beginning.

This:

-(void)aMethod;

Is exactly equivalent to this:

void function(id self, SEL _cmd);

Objective-C's messaging is such that this:

[someObject aMethod];

Is exactly equivalent to this (almost -- there is a variadic argument ABI issue beyond the scope of this answer):

objc_msgSend(someObject, @selector(aMethod));

objc_msgSend() finds the appropriate implementation of the method (by looking it up on someObject) and then, through the magic of a tail call optimization, jumps to the implementation of the method which, for all intents and purposes, works exactly like a C function call that looks like this:

function(someObject, @selector(aMethod));

Quite literally, Objective-C was originally implemented as nothing but a C preprocessor. Anything you can do in Objective-C could be rewritten as straight C.

Doing so, however, would be a complete pain in the ass and not worth your time beyond the incredibly educational experience of doing so.


In general, you use Objective-C methods when talking to objects and function when working with straight C goop. Given that pretty much all of Mac OS X and iOS provide Objective-C APIs -- certainly entirely so for the UI level programming entry points -- then you use Obj-C most of the time.

Even when writing your own model level code that is relatively standalone, you'll typically use Objective-C simply because it provides a very natural glue between state/data & functionality, a fundamental tenant of object oriented programming.

她比我温柔 2024-10-21 18:58:03

在 Objective-C 中,每个函数都对一个对象进行操作,例如

[myObject myFunction]

AC 方法的形式为:

return-type function-name(argument1, argument2, etc) {}

Objective-C 实例方法的格式为:

-(return-type)function-name:argument1 {}

或者对于多argument function

-(return-type)function-name:argument1 function-name:argument2 {}

我总是在 Obj-C 编程中使用 Objective-C 风格的方法,即使你仍然可以使用 C-类型函数也是如此。

我想 C 中 [myObject myMethod:arg] 的等价物可能是 myObject.myMethod(arg)

In Objective-C each function operates on an object, like

[myObject myFunction]

A C method has the form:

return-type function-name(argument1, argument2, etc) {}

An Objective-C instance method has the form:

-(return-type)function-name:argument1 {}

or for a multi-argument function

-(return-type)function-name:argument1 function-name:argument2 {}

I always use Objective-C-style methods in Obj-C programming, even though you can still use C-type functions as well.

I suppose the equivalent in C to [myObject myMethod:arg] might be myObject.myMethod(arg)

半﹌身腐败 2024-10-21 18:58:03

第一个是独立功能。第二个是 Objective-C 类的实例方法。所以我想如果您实际上正在编写课程,您将需要第二个版本。

The first is a freestanding function. The second is an instance method for an Objective-C class. So I guess you would need the second version if you're actually writing a class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文