Can Objective-C protocols and categories be inherited?

后端 未结 5 1725
名媛妹妹
名媛妹妹 2020-12-30 03:13

I am little confused about some concepts around Objective-C protocols and categories.

Can protocols and categories be inherited by subclasses in Objective-C?

5条回答
  •  不知归路
    2020-12-30 03:34

    Your question is unclear. You may be asking whether subclasses inherit the protocols and categories of their superclass. @theAmateurProgrammer has answered this.

    You may also be asking whether categories and protocols can themselves inherit from other categories and protocols. For categories, the answer is no. For protocols, the answer is yes, and in fact protocols should almost always inherit just like classes like this:

    @protocol SomeProtocol 
    ...
    @end
    

    This says that anything that conforms to also conforms to (which is a protocol as well as a class). This allows you to call methods like respondsToSelector:, which is very important for most protocol implementations.

提交回复
热议问题