objective c difference between functions and methods

前端 未结 2 913
一整个雨季
一整个雨季 2020-12-05 00:31

Is there any dramatic difference between functions and methods in Objective -C?

相关标签:
2条回答
  • 2020-12-05 01:00

    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];
    
    0 讨论(0)
  • 2020-12-05 01:01

    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 讨论(0)
提交回复
热议问题