协议和警告的类别“类未实现协议”
好吧,我有这两个协议:
@protocol ivAuthorizationProtocol <NSObject>
-(void)loginReply:(ivSession*)session;
@end
@protocol ivServerListsProtocol <NSObject>
-(void)serverListLoaded:(NSArray*)serverList;
@end
并且有类,
@interface ivClientAppDelegate : NSObject <UIApplicationDelegate>
...
@end
@implementation
...
-(void)authorizeWithLogin:(NSString*)login andPassword:(NSString*)password
{
self.login = login;
self.password = password;
// !!! delegate in the next call should conform to ivAuthorizationProtocol,
// so i get warning "class does not implement protocol ivAuthoriaztionProtocol" here
[_apiWrapper authorizeWith:login andPassword:password forDelegate:self];
}
...
@end
我想将协议方法的实现放在单独的文件(类别)中,以免弄乱主实现文件。例如,用于实现 ivAuthorizationProtocol 的类别标头如下所示:
#import "ivClientAppDelegate.h"
@interface ivClientAppDelegate (ivAuthorizationResponder) <ivAuthorizationProtocol>
-(void)loginReply:(ivSession*)session;
@end
那么,问题是 - 如何摆脱主实现文件中收到的警告?如何告诉编译器符合协议的方法位于类别中? 提前致谢!
Well, I have these two protocols:
@protocol ivAuthorizationProtocol <NSObject>
-(void)loginReply:(ivSession*)session;
@end
@protocol ivServerListsProtocol <NSObject>
-(void)serverListLoaded:(NSArray*)serverList;
@end
and have class
@interface ivClientAppDelegate : NSObject <UIApplicationDelegate>
...
@end
@implementation
...
-(void)authorizeWithLogin:(NSString*)login andPassword:(NSString*)password
{
self.login = login;
self.password = password;
// !!! delegate in the next call should conform to ivAuthorizationProtocol,
// so i get warning "class does not implement protocol ivAuthoriaztionProtocol" here
[_apiWrapper authorizeWith:login andPassword:password forDelegate:self];
}
...
@end
I want to put the implementation of the protocol's methods in separate files (categories) so not to mess up the main implementation file. For example, the header of the category for implementation of ivAuthorizationProtocol looks like this:
#import "ivClientAppDelegate.h"
@interface ivClientAppDelegate (ivAuthorizationResponder) <ivAuthorizationProtocol>
-(void)loginReply:(ivSession*)session;
@end
So, the question is - how can I get rid of the warning I get in the main implementation file? How can I tell the compiler that methods conforming to the protocol are located in categories?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用强制转换来消除警告:
除此之外,您无法在没有收到不同警告(未实现的方法)的情况下执行您想要的操作。通常您会在接口中指定协议:
但是您需要在主
@implementation
块中实现方法。请注意,按照惯例,类和协议名称应以大写字符开头。
You could use a cast to silence the warning:
Other than that, you can't do what you want without getting different warnings (unimplemented methods). Normally you would specify the protocol in the interface:
But then you would need to implement the methods in you main
@implementation
block.Please note that by convention class and protocol names should start with an uppercase character.
你为什么不考虑这样做:
Why don't you consider doing it like this:
将协议方法声明为可选将解决该问题。
Declaring protocol method as optional will fix the issue.