2012年6月26日星期二

Protocol in Extension in Objective C

For cases that a class should implement a Protocol (Interface in some other languages), but may not favorable to be seen by all the class.
A solution could be implementing the protocol at the Category declaration.

For example, I wanna implement the NSXMLParserDelegate in a class but just want the selectors to be
called as delegate.

at .h declaration:
@interface ClassName : NSObject
{
    //sth.....
}
@end

at .m file (or other .h file in some cases) declaring the extension:
@interface ClassName(NSXMLParserDelegate) <NSXMLParserDelegate>
    //some function in the NSXMLParserDelegate protocol.....
@end

@implementation ClassName(NSXMLParserDelegate)
    //do sth...
@end

@implementation ClassName
    //.......
@end

Reference: Stackoverflow

沒有留言:

發佈留言