views:

54

answers:

3

If I have an protocol (say UIPickerViewDataSource) and I implement its required methods, do I need to declare those methods in the header file of my class?

At the moment I'm not doing so and I get a warning of incomplete implementation (although everything works fine). If I do add the required methods in the then I don't get such warning:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;

Is this the correct behaviour? Is it really necessary to add the declaration for required protocol methods in the header file of my class?

+3  A: 

No, you don't. Declaring that the class implements that protocol and implementing the methods is enough. You could still declare them in the header for documentation purposes, though.

Ferruccio
+1 If you specify the implementing protocol in the header, I wouldn't declare them there - same with overridden methods. I do like the grouping with pragmas in the implementation file as Apple suggests.
Eiko
Appart from the unnecessity to explicitly specify the protocol methods in the header, I might add that most of the time I find adding those protocol methods to a class header unnecessarily litters the header file and distracts from the methods that make the class "unique".
DarkDust
Indeed. It's the open specification of what methods your class "exports". And the delegate methods are probably not what you want other people/objects to call directly.
Eiko
+1  A: 

The correct way is to declare that your class implements the protocol. If for instance your class is called LordSandwichViewController, then your class interface must look like this:

@interface LordSandwichViewController : UIViewController <UIPickerViewDataSource> {
{
}

So you don't declare the protocol methods in your class interface, only the protocol.

Rits
To be exact, you use the angle-bracket-delimited portion of the interface declaration to declare that your class *conforms to* the listed protocols. An actual protocol declaration begins with the `@protocol` directive.
Jeremy W. Sherman
A: 

Incomplete implementation warnings tell you that you are not implementing all the required methods you are either:

  1. Defining in your header.
  2. Declaring a method required by a protocol you're conforming to.

Look at what methods it's expecting, and implement those.

jer