一个类别可以在目标 C 中实现一个协议吗?

我在 NSDate 上有一个分类,如果它可以实现我以前创建的一个协议,那将是很方便的。这可能吗?这句话的正确语法是什么?

18265 次浏览

Yes, that's possible. The syntax is:

@interface NSDate (CategoryName) <ProtocolName>
@end


@implementation NSDate (CategoryName)
@end

Here's Apple's documentation on the topic.

It's also possible to do this using a class extension. I very much like this to privately conform to delegate protocols. Doing so hides the implementation detail of being some delegate of some class from the public interface and removes the dependency from the header.