开发者

objective c difference between functions and methods

Is th开发者_JAVA技巧ere any dramatic difference between functions and methods in Objective -C?


First, I'm a beginner in Objective-C, but I can say what I know.

Functions are code blocks that are unrelated to an object / class, just inherited from c, and you call them in the way:

// declaration
int fooFunction() {
    return 0;
}

// call
int a;
a = fooFunction();

While methods are attached to class / instance (object) and you have to tell the class / object to perform them:

// declaration
- (int)fooMethod {
    return 0;
}

// call
int a;
a = [someObjectOfThisClass fooMethod];


It is even simpler; a method is just a C function with the first two argument being the target of the method call and the selector being called, respectively.

I.e. every single method call site can be re-written as an equivalent C function call with absolutely no difference in behavior.


In depth answer here: Why [object doSomething] and not [*object doSomething]? Start with the paragraph that says "Getting back to the C preprocessor roots of the language, you can translate every method call to an equivalent line of C".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜