S.J. Lim
S.J. Lim

Reputation: 3165

Need declare protocol in objective c?

I wonder if it is needed to declare the protocol.

So, I delete a part that protocol declare code.

I thought that it will happen compile error.

but, Has been running without any problems.

Why need protocol declare part?

#import <UIKit/UIKit.h>


@interface SingleComponentPickerViewController : UIViewController {
// <UIPickerViewDelegate, UIPickerViewDataSource> {   ==> protocol declare part
    IBOutlet    UIPickerView *singlePicker;
                NSArray *pickerData;
}
@property (nonatomic, retain) UIPickerView *singlePicker;
@property (nonatomic, retain) NSArray *pickerData;
- (IBAction)buttonPressed:(id)sender;
@end


<UIPickerViewDelegate, UIPickerViewDataSource> <= this is what feature?
                                                  When omit this, excute without any problem. 

Below is picture After compiled screen.

It has no issues.

And excute without any problems..

enter image description here

Upvotes: 2

Views: 344

Answers (2)

Suhail Patel
Suhail Patel

Reputation: 13694

You are not declaring the Protocol here, you are merely specifying that the your class SingleComponentPickerViewController follows the protocols you specify.

The protocol needs to be specified if you are using any components and are setting you SingleComponentPickerViewController as their delegate. This is so the compiler knows that your class follows that specific protocol and you have implemented any required protocol methods.

Upvotes: 1

Macmade
Macmade

Reputation: 54059

<UIPickerViewDelegate, UIPickerViewDataSource>

It doesn't declare a protocol, it tells the compiler that your class conforms to the listed protocols.

It's needed, so other classes can know your class conforms to their protocol(s). If you omit them, you won't get a compiler error, but the delegate methods you declared in your class may not be called.

Upvotes: 0

Related Questions