Protocol versus Category

后端 未结 7 1564
忘了有多久
忘了有多久 2020-12-12 14:09

Can anyone explain the differences between Protocols and Categories in Objective-C? When do you use one over the other?

7条回答
  •  -上瘾入骨i
    2020-12-12 14:35

    A protocol allows you to declare a list of methods which are not confined to any particular class or categories. The methods declared in the protocol can be adopted any class/categories. A class or category which adopts a protocol must implements all the required methods declared in the protocol.

    A category allows you to add additional methods to an existing class but they do not allow additional instance variables. The methods the category adds become part of the class type.

提交回复
热议问题