协议和警告的类别“类未实现协议”

发布于 2024-11-19 23:48:52 字数 1107 浏览 2 评论 0原文

好吧,我有这两个协议:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

金兰素衣 2024-11-26 23:48:53

您可以使用强制转换来消除警告:

[_apiWrapper
    authorizeWith:login
    andPassword:password
    forDelegate:(id<ivAuthorizationProtocol>)self
];

除此之外,您无法在没有收到不同警告(未实现的方法)的情况下执行您想要的操作。通常您会在接口中指定协议:

@interface ivClientAppDelegate : NSObject <UIApplicationDelegate,ivAuthorizationProtocol

但是您需要在主 @implementation 块中实现方法。

请注意,按照惯例,类和协议名称应以大写字符开头。

You could use a cast to silence the warning:

[_apiWrapper
    authorizeWith:login
    andPassword:password
    forDelegate:(id<ivAuthorizationProtocol>)self
];

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:

@interface ivClientAppDelegate : NSObject <UIApplicationDelegate,ivAuthorizationProtocol

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.

一念一轮回 2024-11-26 23:48:53

你为什么不考虑这样做:

         @interface ivClientAppDelegate : NSObject <UIApplicationDelegate, ivAuthorizationProtocol >

Why don't you consider doing it like this:

         @interface ivClientAppDelegate : NSObject <UIApplicationDelegate, ivAuthorizationProtocol >
蓝颜夕 2024-11-26 23:48:53

将协议方法声明为可选将解决该问题。

Declaring protocol method as optional will fix the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文