I have a category on NSDate and it would be convenient if it could implement a protocol I previously created. Is this possible? what's the correct syntax for this?
Asked
Active
Viewed 1.9k times
1 Answers
154
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.

Nikolai Ruhe
- 81,520
- 17
- 180
- 200
-
1Too bad there isn't a way to move the protocol declaration to the class category's implementation. – adib Nov 30 '12 at 05:18
-
@adib That's what class extensions are there for. – Nikolai Ruhe Nov 30 '12 at 09:24
-
@bikram990 The document has been retired and is now in the legacy section. I updated the link. – Nikolai Ruhe Jun 12 '14 at 13:25
-
unfortunately we can not declare instance variables in categories, that we might need for some delegate protocol implementations – yasirmturk Sep 06 '16 at 07:19
-
@yasirmturk You could do that using objc_setAssociatedObject :) – animaonline Oct 24 '16 at 11:20