Can a category implement a protocol in Objective C?

Objective CCocoaProtocolsCategories

Objective C Problem Overview


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?

Objective C Solutions


Solution 1 - Objective C

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncfischerView Question on Stackoverflow
Solution 1 - Objective CNikolai RuheView Answer on Stackoverflow