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

后端 未结 12 1812
小鲜肉
小鲜肉 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:19

    There isn't really a "private method" in Objective-C, if the runtime can work out which implementation to use it will do it. But that's not to say that there aren't methods which aren't part of the documented interface. For those methods I think that a category is fine. Rather than putting the @interface at the top of the .m file like your point 2, I'd put it into its own .h file. A convention I follow (and have seen elsewhere, I think it's an Apple convention as Xcode now gives automatic support for it) is to name such a file after its class and category with a + separating them, so @interface GLObject (PrivateMethods) can be found in GLObject+PrivateMethods.h. The reason for providing the header file is so that you can import it in your unit test classes :-).

    By the way, as far as implementing/defining methods near the end of the .m file is concerned, you can do that with a category by implementing the category at the bottom of the .m file:

    @implementation GLObject(PrivateMethods)
    - (void)secretFeature;
    @end
    

    or with a class extension (the thing you call an "empty category"), just define those methods last. Objective-C methods can be defined and used in any order in the implementation, so there's nothing to stop you putting the "private" methods at the end of the file.

    Even with class extensions I will often create a separate header (GLObject+Extension.h) so that I can use those methods if required, mimicking "friend" or "protected" visibility.

    Since this answer was originally written, the clang compiler has started doing two passes for Objective-C methods. This means you can avoid declaring your "private" methods completely, and whether they're above or below the calling site they'll be found by the compiler.

    0 讨论(0)
  • 2020-11-22 15:19

    One more thing that I haven't seen mentioned here - Xcode supports .h files with "_private" in the name. Let's say you have a class MyClass - you have MyClass.m and MyClass.h and now you can also have MyClass_private.h. Xcode will recognize this and include it in the list of "Counterparts" in the Assistant Editor.

    //MyClass.m
    #import "MyClass.h"
    #import "MyClass_private.h"
    
    0 讨论(0)
  • 2020-11-22 15:22

    There isn't, as others have already said, such a thing as a private method in Objective-C. However, starting in Objective-C 2.0 (meaning Mac OS X Leopard, iPhone OS 2.0, and later) you can create a category with an empty name (i.e. @interface MyClass ()) called Class Extension. What's unique about a class extension is that the method implementations must go in the same @implementation MyClass as the public methods. So I structure my classes like this:

    In the .h file:

    @interface MyClass {
        // My Instance Variables
    }
    
    - (void)myPublicMethod;
    
    @end
    

    And in the .m file:

    @interface MyClass()
    
    - (void)myPrivateMethod;
    
    @end
    
    @implementation MyClass
    
    - (void)myPublicMethod {
        // Implementation goes here
    }
    
    - (void)myPrivateMethod {
        // Implementation goes here
    }
    
    @end
    

    I think the greatest advantage of this approach is that it allows you to group your method implementations by functionality, not by the (sometimes arbitrary) public/private distinction.

    0 讨论(0)
  • 2020-11-22 15:23

    every objects in Objective C conform to NSObject protocol, which holds onto the performSelector: method. I was also previously looking for a way to create some "helper or private" methods that I did not need exposed on a public level. If you want to create a private method with no overhead and not having to define it in your header file then give this a shot...

    define the your method with a similar signature as the code below...

    -(void)myHelperMethod: (id) sender{
         // code here...
    }
    

    then when you need to reference the method simply call it as a selector...

    [self performSelector:@selector(myHelperMethod:)];
    

    this line of code will invoke the method you created and not have an annoying warning about not having it defined in the header file.

    0 讨论(0)
  • 2020-11-22 15:27

    There is a benefit of private methods absence. You can move the logic that you intended to hide to the separate class and use it as delegate. In this case you can mark delegate object as private and it will not be visible from outside. Moving logic to the separate class (maybe several) makes better design of your project. Cause your classes become simpler and your methods are grouped in classes with proper names.

    0 讨论(0)
  • 2020-11-22 15:30

    There's no way of getting around issue #2. That's just the way the C compiler (and hence the Objective-C compiler) work. If you use the XCode editor, the function popup should make it easy to navigate the @interface and @implementation blocks in the file.

    0 讨论(0)
提交回复
热议问题