Categories vs. Unofficial Protocols

I think I understand the difference between (formal) protocols and categories. Now, if I understand correctly, informal protocols should be categories (usually defined in NSObject) that are used for certain purposes (perhaps to enable only a part of the methods listed in it to be implemented, as opposed to formal protocols). I have to be sure of this: can anyone confirm that the informal protocol is simply a category (or explains the differences)? Thanks.

+4
source share
4 answers

A category is an extension to the functionality of a class - it is an implementation of some methods:

@interface NSObject (MyCategory) - (void)doSomething; @end ... @implementation NSObject (MyCategory) - (void)doSomething { // do something... } @end 

A formal protocol is something completely different. If you are familiar with any other object-oriented language, then it looks like an interface (in Java, C ++, C #, etc.).
A protocol can be attached to any class implementation as follows:

 @protocol MyProtocol @required - (void)doSomething; @optional - (void)doSomethingOptional; @end ... @interface MyClass : NSObject <MyProtocol> { } @end ... @implementation MyClass - (void)doSomething { // do something... } @end 

According to the documentation, informal protocols are categories of the NSObject class (I never used this approach):

 @interface NSObject (MyInformalProtocol) - (void)doSomething; @end ... @implementation NSObject (MyInformalProtocol) - (void)doSomething { // do something... } @end 
+8
source

Informal protocols are really just categories defined in NSObject, but if you define a delegate interface, there is a better way. Instead, use formal protocols (i.e. real, actual @protocol definitions) with additional methods. This allows you to check the type of delegate (that is, it actually matches the protocol) at compile time and receive a warning if you use the wrong object.

 @protocol GLFunkyObjectDelegate @optional -(void)funkyObject: (GLFunkyObject *)obj willDoSomething: (GLSomeThing *)thing; -(void)funkyObject: (GLFunkyObject *)obj didDoSomething: (GLSomeThing *)thing; @end @interface GLFunkyObject { id <GLFunkyObjectDelegate> delegate; } //... @end 
+5
source

The Objective-C manual states that you are right - informal protocols are usually categories.

In addition to formal protocols, you can also define an informal protocol by grouping methods in a category declaration:

However, you can implement an unofficial protocol by simply agreeing to it by word of mouth - of course, I was never tempted to look for a category when implementing delegation methods for an interface builder; and Categories are used for other things besides informal protocols.

+3
source

A protocol is an interface definition ONLY.

A category (against NSObject) is both the definition of an interface and the implementation of that interface, which inherits all subclasses (NSObject), if they do not override the methods of the category.

While Apple has chosen in the past the definition of informal protocols by creating categories for NSObject, this does not mean that it is a definition of another.

The fact that you can semi-define a category by declaring an interface but not implementing it should (in a fair and fair world) cause compilation errors, since the Objective-C compiler definitely complains about the "incomplete implementation" for real interfaces. But they have been abused long enough so that you can expect that the behavior will not change.

0
source

Source: https://habr.com/ru/post/1300036/


All Articles