Can Objective-C protocols and categories be inherited?

后端 未结 5 1728
名媛妹妹
名媛妹妹 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:48

    Categories are like an extension to a class. You can add your own methods to other classes (such as NSString or anything else). Which means that any subclass gets the methods as well.

    Whereas a protocol are a list of methods that requires the class that confirms to it to implement all of it (unless it uses the @optional tag). So there is no point of it's subclass to inherit it.

    Edit:

    For the protocol implementation, I realized I wasn't clear enough. Protocol Methods that were implemented in it's superclass can be inherited, however, what I meant was usually you don't need to override your superclass's protocol method.

提交回复
热议问题