目标C:类扩展和协议构造警告

Ben*_*ves 3 iphone objective-c

我有一个大类,为了便于阅读,我将其分为几个不同的类扩展文件.

@protocol MyProtocol
@required
-(void)required;
@end

@interface MyClass : NSObject <MyProtocol>
@end

@interface MyClass (RequiredExtension)
-(void)required;
@end
Run Code Online (Sandbox Code Playgroud)

没有编译器警告,有没有更好的方法来做到这一点?

 warning: class 'MyClass' does not fully implement the 'MyProtocol' protocol
Run Code Online (Sandbox Code Playgroud)

Tec*_*Zen 14

为每个协议实现使用类别.当我有复杂的viewControllers时,我会使用它.

例如,我有一个实现NSTextDelegate协议的类别.

所以,MyComplexViewController + NSTextDelegate.h:

#import "MyComplexViewController.h"

@interface MyComplexViewController (NSTextDelegate) <NSTextDelegate>

@end
Run Code Online (Sandbox Code Playgroud)

和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
Run Code Online (Sandbox Code Playgroud)

然后我获取主类定义和类别的所有标题,并将它们组合成一个标题,然后我导入我需要使用该类的位置.