开发者

implementing protocol methods in objective-c

开发者 https://www.devze.com 2023-01-20 16:39 出处:网络
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?

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:(UIP开发者_如何学运维ickerView *)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?


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号