Best way to define private methods for a class in Objective-C

后端 未结 12 1960
小鲜肉
小鲜肉 2020-11-22 14:34

I just started programming Objective-C and, having a background in Java, wonder how people writing Objective-C programs deal with private methods.

I understand there

12条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 15:09

    You could try defining a static function below or above your implementation that takes a pointer to your instance. It will be able to access any of your instances variables.

    //.h file
    @interface MyClass : Object
    {
        int test;
    }
    - (void) someMethod: anArg;
    
    @end
    
    
    //.m file    
    @implementation MyClass
    
    static void somePrivateMethod (MyClass *myClass, id anArg)
    {
        fprintf (stderr, "MyClass (%d) was passed %p", myClass->test, anArg);
    }
    
    
    - (void) someMethod: (id) anArg
    {
        somePrivateMethod (self, anArg);
    }
    
    @end
    

提交回复
热议问题