Specifically, I would like to share a set of utility methods between NSTreeNode and my class which is not a subclass of NSTreeNode. My class (WCTreeNode) inherits from WCObject, which is a subclass of NSObject.
My current solution is to have the methods declared in the header of my class (WCTreeNode) and then again in a category on NSTreeNode. However, I’m not particularly fond of this because whenever I make changes, I have to make sure to do it in both files.
I realize I could just make a category on NSObject and list the methods there, but that doesn’t seem specific enough to me and doesn’t let the compiler help me as much with respect to type-checking.
I’d really like a solution that allows me to keep the code in a single file so I don’t have to change things in multiple places each time.
Any suggestions?
I think a protocol is the way to go here. Put your custom methods in a protocol, and subclass
NSTreeNode; the only customization this subclass would make would be to adopt the protocol.WCTreeNodecan also adopt the protocol. This allows type-checking and keeps the methods in one place.