Stanley
Stanley

Reputation: 4486

A question about the effect of Delegate Protocol declaration

Consider the following header file, there are 2 Delegate Protocols declaration : NSSpeechSynthesizerDelegate and NSTableViewDelegate. But after I comment off the two Delegates the program still compile and run perfectly. My question is : what is the real effect of adding the 2 delegate declaration ?

#import <Cocoa/Cocoa.h>


@interface  AppController : NSObject 
       <NSSpeechSynthesizerDelegate, NSTableViewDelegate>

{

IBOutlet NSTextField *textField;
IBOutlet NSButton    *stopButton;
IBOutlet NSButton    *startButton;
IBOutlet NSTableView *tableView;
         NSArray     *voiceList; 

NSSpeechSynthesizer  *speechSynth;  
}


- (IBAction) sayIt  : (id) sender;
- (IBAction) stopIt : (id) sender;
- (void)     speechSynthesizer : (NSSpeechSynthesizer *) sender 
     didFinishSpeaking : (BOOL) complete;
- (int)      numberOfRowsInTableView   : (NSTableView *) tv;
- (id)       tableView                 : (NSTableView *) tv
             objectValueForTableColumn : (NSTableColumn *) tableColumn
                       row : (int) row;
- (void) tableViewSelectionDidChange   : (NSNotification *) notification;
- (void) awakeFromNib;

@end

Upvotes: 1

Views: 202

Answers (1)

Matthias Bauch
Matthias Bauch

Reputation: 90117

Afaik there is only a effect for the developer, the code won't change.
If you like compiler warnings and if you are sure you implement the required methods you can live without declaring that you support the protocol.

There are no required methods in NSTableViewDelegate or NSSpeechSynthesizerDelegate, so you don't get warnings about missing functions, so it might be not so obvious.


For me these are the main reasons for using protocols:

  1. You get a compiler warning if you forget to implement a method that is required by the protocol.
  2. Code sense only suggests method names if they are found in a protocol
  3. It's the law.

Upvotes: 1

Related Questions