Search code examples
objective-ccocoaprivate-methodsobjective-c-category

Private methods in a Category, using an anonymous category


I'm creating a category over NSDate. It has some utility methods, that shouldn't be part of the public interface.

How can I make them private?

I tend to use the "anonymous category" trick when creating private methods in a class:

@interface Foo()
@property(readwrite, copy) NSString *bar;
- (void) superSecretInternalSaucing;
@end

@implementation Foo
@synthesize bar;
.... must implement the two methods or compiler will warn ....
@end

but it doesn't seem to work inside another Category:

@interface NSDate_Comparing() // This won't work at all
@end

@implementation NSDate (NSDate_Comparing)

@end

What's the best way to have private methods in a Category?


Solution

  • It should be like this:

    @interface NSDate ()
    
    @end
    
    @implementation NSDate (NSDate_Comparing)
    
    @end