Ios – Must Protocols Conform To The NSObject Protocol


The NSObject protocol comes with the stock protocol templates, but it doesn't appear to be all that necessary for actual implementations of the protocol. Leaving it out seems to change absolutely nothing. So, is it really necessary for a protocol to inherit from it, or is it just an unnecessary add-on?

Best Solution

For years I (and many like me) didn't make our protocols conform to <NSObject>. It works fine. But it can often be annoying. The most common annoyance is that you can't use respondsToSelector: without casting back to NSObject* (which kind of defeats the whole point of a protocol). That didn't matter back in the ObjC1 days because there was no @optional, so none of us worried about it (we didn't use protocols much at all in those days since without @optional they weren't that useful). Then ObjC2 came along with the wonderful addition of optional methods and suddenly respondsToSelector: mattered. It took a little while for the slower of us, but eventually we started to figure out that life was much simpler if you make your protocols conform to <NSObject>. Blessedly this has now made its way into Xcode, making it easier for everyone to do things in the more convenient way.

But no, you don't have to do it. It doesn't matter in many cases. But there's not much reason not to do it, so I recommend it.