Protocol versus Category

2019-01-21 01:19发布

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

7条回答
叼着烟拽天下
2楼-- · 2019-01-21 02:10

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.

查看更多
登录 后发表回答