How to call an Objective-C Method from a C Method?

后端 未结 5 1006
慢半拍i
慢半拍i 2020-11-28 06:44

I have an Obj-C object with a bunch of methods inside of it. Sometimes a method needs to call another method inside the same object. I can\'t seem to figure out how to get

5条回答
  •  攒了一身酷
    2020-11-28 07:42

    In order for that to work, you should define the C method like this:

    void cMethod(id param);
    

    and when you call it, call it like this:

    cMethod(self);
    

    then, you would be able to write:

    [param objcMethod];
    

    In your cMethod.

    This is because the self variable is a special parameter passed to Objective-C methods automatically. Since C methods don't enjoy this privilege, if you want to use self you have to send it yourself.

    See more in the Method Implementation section of the programming guide.

提交回复
热议问题