Can a category implement a protocol in Objective C?

≯℡__Kan透↙ 提交于 2019-12-17 21:43:23

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/5824755/can-a-category-implement-a-protocol-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!