C 函数与 Objective-C 方法?
两者有什么区别?如果我正在编写一个程序,我什么时候需要 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上,Objective-C 方法只是一个始终在开头出现两个参数的 C 函数。
这:
完全等价于:
Objective-C 的消息传递是这样的:
完全等价于这个(几乎 - 有一个可变参数 ABI 问题超出了这个答案的范围):
objc_msgSend() 找到适当的实现方法(通过在
someObject
上查找),然后通过尾部调用优化的魔力,跳转到该方法的实现,无论出于何种目的,该方法的工作方式与 C 函数调用完全相同看起来像这样:从字面上看,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:
Is exactly equivalent to this:
Objective-C's messaging is such that this:
Is exactly equivalent to this (almost -- there is a variadic argument ABI issue beyond the scope of this answer):
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: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.
在 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 bemyObject.myMethod(arg)
第一个是独立功能。第二个是 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.