开发者

categories for protocols and warnings "class does not implement protocol"

开发者 https://www.devze.com 2023-03-21 00:17 出处:网络
Well, I have these two protocols: @protocol ivAuthorizationProtocol <NSObject> -(void)loginReply:(ivSession*)session;

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 categor开发者_StackOverflow中文版y 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!


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.


Why don't you consider doing it like this:

         @interface ivClientAppDelegate : NSObject <UIApplicationDelegate, ivAuthorizationProtocol >


Declaring protocol method as optional will fix the issue.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号