Protocol versus Category

后端 未结 7 1560
忘了有多久
忘了有多久 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条回答
  • 2020-12-12 14:45

    A protocol is the same thing as an interface in Java: it's essentially a contract that says, "Any class that implements this protocol will also implement these methods."

    A category, on the other hand, just binds methods to a class. For example, in Cocoa, I can create a category for NSObject that will allow me to add methods to the NSObject class (and, of course, all subclasses), even though I don't really have access to NSObject.

    To summarize: a protocol specifies what methods a class will implement; a category adds methods to an existing class.

    The proper use of each, then, should be clear: Use protocols to declare a set of methods that a class must implement, and use categories to add methods to an existing class.

    0 讨论(0)
提交回复
热议问题