Objective C: Class Extensions and Protocol Conformation Warnings
I have a large c开发者_StackOverflowlass, which I have divided into several different class extension files for readability.
@protocol MyProtocol
@required
-(void)required;
@end
@interface MyClass : NSObject <MyProtocol>
@end
@interface MyClass (RequiredExtension)
-(void)required;
@end
Is there a better way to do this, without the compiler warning?
warning: class 'MyClass' does not fully implement the 'MyProtocol' protocol
Use a category for each protocol implementation. I use this when I have complex viewControllers.
For example, I have a category that implements NSTextDelegate protocol.
So, MyComplexViewController+NSTextDelegate.h:
#import "MyComplexViewController.h"
@interface MyComplexViewController (NSTextDelegate) <NSTextDelegate>
@end
and MyComplexViewController+NSTextDelegate.m:
#import "MyComplexViewController+NSTextDelegate.h"
@implementation MyComplexViewController (NSTextDelegate)
- (BOOL)textShouldBeginEditing:(NSText *)textObject{
...
}
- (BOOL)textShouldEndEditing:(NSText *)textObject{
...
}
- (void)textDidBeginEditing:(NSNotification *)notification{
...
}
- (void)textDidEndEditing:(NSNotification *)notification{
...
}
- (void)textDidChange:(NSNotification *)notification{
....
}
@end
Then I take all the headers for the main class definition and the categories and combine them into one header which I then import where I need to use the class.
@interface MyClass : NSObject
@end
@interface MyClass (RequiredExtension) <MyProtocol>
-(void)required;
@end
Adopt the protocol in the category.
You don't need to change your style of coding. To get around the warning, it only need to implement "required" method of the protocol, not "optional"
If that's only for readability, you should use categories only. A protocol is not needed in such a case.
精彩评论