使类符合现有方法的类别协议
我有一个名为 MyProtocol 的协议。 MyProtocol 有一个必需的方法:
- (NSUInteger)length;
和一些其他方法。
现在我想让 NSString 类符合 MyProtocol 的类别。像这样:
@interface NSString (NSStringWithMyProtocol) <MyProtocol>
@end
在这个类别中,我实现了除“length”方法之外的所有方法,因为我想要原始的 NSString 实现。 我不想在这个特定的类中覆盖它。
现在,由于类别中 MyProtocol 的实现不完整,我收到警告。
我知道有一些解决方案可以解决这个问题。
- 使方法可选
- Pointer Swizzling
- 将子类添加到符合协议的类中。然后省略实现。
我不想使用这些选项,因为它们会导致我的其余代码设计不佳。
选项 3 不好,因为现有的直接子类将不符合协议。
有谁知道如何在不实施长度方法的情况下删除警告?
注意:类、类别和协议只是示例。我确实在其他课程中遇到了这个问题,但我无法发布。 谢谢
编辑:添加了第三个选项。
完整代码:
协议:
@protocol MyProtocol <NSObject>
- (void) myMethod;
- (NSInteger) length;
@end
类别标头:
#import <Foundation/Foundation.h>
#import "MyProtocol.h"
@interface NSString (MyProtocol) <MyProtocol>
@end
类别实现:
@implementation NSString (MyProtocol)
- (void)myMethod {
}
@end
这会导致以下警告。
Incomplete implementation
Method in protocol not implemented
在此屏幕截图中,您可以看到我的警告:
我尝试使用 LLVM GCC 4.2 和 Apple LLVM 3.0 编译器进行编译。 我还在 xcode 4.0.2 和 Xcode 4.2 上编译。 我使用的是 OS X 10.6.8。
I have a protocol named MyProtocol.
MyProtocol has an required method:
- (NSUInteger)length;
And some other methods.
Now i want to make the NSString class conform to MyProtocol with a category. Like so:
@interface NSString (NSStringWithMyProtocol) <MyProtocol>
@end
In this category i implement all methods excluding the 'length' method, because i want the original NSString implementation.
I do not want to override it in this particular class.
Now i get a warning because of an incomplete implementation of MyProtocol in the category.
I know there are a few solutions to solve this.
- Make the method optional
- Pointer Swizzling
- Adding subclass to class which is conform to the protocol. Then leave out the implementation.
I do not want to use these options because they result in a bad design for the rest of my code.
Option 3 is bad, because of existing direct subclasses will not be conform to the protocol.
Does anybody know how to remove the warning without implementing the length method?
NOTE: The class, category and protocol are just examples. I did encounter this problem with other classes which i could not post about.
Thanks
EDIT: Added the third option.
Full Code:
The protocol:
@protocol MyProtocol <NSObject>
- (void) myMethod;
- (NSInteger) length;
@end
The category header:
#import <Foundation/Foundation.h>
#import "MyProtocol.h"
@interface NSString (MyProtocol) <MyProtocol>
@end
The category implementation:
@implementation NSString (MyProtocol)
- (void)myMethod {
}
@end
This results in the following warnings.
Incomplete implementation
Method in protocol not implemented
In this screenshot you can see my warning:
I tried compiling with LLVM GCC 4.2 and the Apple LLVM 3.0 compiler.
I also compiled on xcode 4.0.2 and Xcode 4.2.
I'm on OS X 10.6.8.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我无法重现这个问题。您可以发布演示它的代码吗?以下内容在 10.7 上编译时不会出现警告。
I cannot reproduce this issue. Can you post code that demonstrates it? The following compiles without warnings on 10.7.
您可能会认为这有点像黑客,我不知道它是否会起作用,但是如何将其声明为只读属性
,然后在您的类别的实现中,将其设为
@dynamic
You might consider this a bit of a hack, and I don't know if it will even work, but how about declaring it as a read only property
and then in the implementation of your category, make it
@dynamic