使类符合协议与现有方法的类别 [英] Making class conform to protocol with category for existing methods

查看:213
本文介绍了使类符合协议与现有方法的类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为MyProtocol的协议。
MyProtocol有一个必需的方法:

   - (NSUInteger)length; 

还有其他一些方法。



我想使NSString类符合MyProtocol类别。像这样:

  @interface NSString(NSStringWithMyProtocol)< MyProtocol> 
@end

在这个类别中,我实现不包括'length'方法的所有方法,因为我想要原始的NSString实现。
我不想在这个特定的类中重写它。



现在我收到一个警告,因为MyProtocol在该类别中不完整。



我知道有几个解决方案可以解决这个问题。


  1. 使方法可选

  2. 指针swizzling

  3. 将子类添加到符合协议的类中。然后不要执行。

我不想使用这些选项,因为我们的代码的其余部分导致错误的设计。

选项3是坏的,因为现有的直接子类将不符合协议。



有没有人知道如何删除警告没有实现长度方法?



注意:类,类别和协议只是示例。我没有遇到这个问题与其他类,我不能发布。
谢谢



编辑:添加了第三个选项。



全部代码



协议:

  @protocol MyProtocol< NSObject> 

- (void)myMethod;
- (NSInteger)length;

@end

类别标题:

  #import< Foundation / Foundation.h> 
#importMyProtocol.h

@interface NSString(MyProtocol)&MyProtocol>
@end

类别实现:

  @implementation NSString(MyProtocol)

- (void)myMethod {

}

@end

这将导致以下警告。

 不完整的实现

未实现协议的方法

在此屏幕截图中,您可以看到我的警告:



我尝试使用LLVM GCC 4.2和Apple LLVM 3.0编译器进行编译。
我也编译在xcode 4.0.2和Xcode 4.2上。
我在OS X 10.6.8上。

解决方案

我无法重现此问题。你可以发表演示的代码吗?以下编译没有警告10.7。

  #import< Foundation / Foundation.h> 

@protocol MyProtocol< NSObject>
- (NSUInteger)length;
@end

@interface NSString(NSStringWithMyProtocol)< MyProtocol>
@end

int main(int argc,const char * argv []){
@autoreleasepool {
id< MyProtocol> foo = @foo;
NSLog(@%@,foo);
}
return 0;
}


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.

  1. Make the method optional
  2. Pointer Swizzling
  3. 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.

解决方案

I cannot reproduce this issue. Can you post code that demonstrates it? The following compiles without warnings on 10.7.

#import <Foundation/Foundation.h>

@protocol MyProtocol <NSObject>
- (NSUInteger)length;
@end

@interface NSString (NSStringWithMyProtocol) <MyProtocol>
@end

int main (int argc, const char * argv[]) {
  @autoreleasepool {
    id<MyProtocol> foo = @"foo";
    NSLog(@"%@", foo);    
  }
  return 0;
}

这篇关于使类符合协议与现有方法的类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆