使用匿名类别的类别中的私有方法

cfi*_*her 7 cocoa objective-c private-methods objective-c-category

我正在创建一个超过NSDate的类别.它有一些实用方法,不应该是公共接口的一部分.

我怎样才能让它们变成私密的?

在类中创建私有方法时,我倾向于使用"匿名类别"技巧:

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

@implementation Foo
@synthesize bar;
.... must implement the two methods or compiler will warn ....
@end
Run Code Online (Sandbox Code Playgroud)

但它似乎不适用于另一个类别:

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

@implementation NSDate (NSDate_Comparing)

@end
Run Code Online (Sandbox Code Playgroud)

在类别中使用私有方法的最佳方法是什么?

Pau*_*nov 4

应该是这样的:

@interface NSDate ()

@end

@implementation NSDate (NSDate_Comparing)

@end
Run Code Online (Sandbox Code Playgroud)

  • 在这种情况下,它显示一条警告“类别正在实现一个方法,该方法也将由其主类实现”。 (8认同)