I see that the writer does not declare in the header file the protocol methods that the MyDocument class implements. Shouldn’t we declare these methods in the header file?
I was wondering this too …
The shorter answer: It would be perfectly acceptable to declare implemented protocol methods in the header file, but it’s not necessary, and the general custom is not to do so.
The longer answer:
If one object inherits from another, you don’t need to list in the
@interface any methods that are overridden (though you can if you wish). A @MyDocument@ object will respond to any method that an @NSDocument@ object will respond to, whether it is overridden by @MyDocument@, or just inherited from @NSDocument@. And, in turn, each of @NSDocument@’s methods will either be listed in the
@interface for @NSDocument@, or in the interface for an object further up the inheritance chain, like @NSObject@.
If an object’s interface defines that it adopts a certain protocol, this means that the object will implement any required methods in that protocol, as well as some or all of any optional methods. When you compile a project, the compiler will complain (with a warning) if you fail to implement a required method. Alternatively, if you have an object whose interface doesn’t declare it as adopting a protocol, and then you try to supply that object as an argument to a method that expects the protocol to be adopted, you will again receive a warning. (Note that if you’re making connections with Interface Builder, you probably won’t receive warnings, though; warnings will only be generated if you’re using methods like @setDataSource:@.)
A protocol is just a list of related methods. It’s faster to indicate that you adopt a protocol than declare that you implement a whole list of methods for every object that you wish to use as an @NSTableViewDataSource@, for example.
In practice, certainly while you’re still exploring the basics, you may find it helpful to think that in general:
- protocols are used to tell some other object which methods your own object may or may not implement (such as data source, or delegate methods).
- the list of methods in the
@interfaceis used to tell humans which methods your object implements
Both protocols and
@interface declarations are used by the compiler to generate warnings at compile time if you send a message to an object that it doesn’t think can handle that message. At runtime, it doesn’t matter what’s included in the header file—if you send a message to an object, and that object implements the relevant method, the method will be called, even if it’s not listed in the
@interface. If you send a message to an object that doesn’t implement the relevant method, you’ll get an exception regarding an “unknown selector.”
This is an over simplification because, for example, one object can ask another if it implements a protocol using the @conformsToProtocol:@ method. If the protocol isn’t adopted in the
@interface, the response will be negative, even if the object actually implements all the required methods. Equally, the response will be positive if the protocol is adopted in the
@interface, even if the object doesn’t implement any of the required methods.