Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring protocol like @class

Tags:

objective-c

I have two protocols communicating with each other. They are defined in the same file.

@protocol Protocol1 <NSObject>
-(void)setProtocolDelegate:(id<Protocol2>)delegate;
@end

@protocol Protocol2 <NSObject>
-(void)protocol:(UIViewController<Protocol1>*)anObject chosenElementAtIndex:(NSInteger)aIndex;
@end

How to declare an empty protocol Protocol2 just to let know compiler that it is declared later?

If Protocol2 was a class I'd write @class Protocol2; beforewards.

@class Protocol2;
@protocol Protocol1 <NSObject>
-(void)setProtocolDelegate:(Protocol2*)delegate;
@end

@interface Protocol2 <NSObject>
-(void)protocol:(UIViewController<Protocol1>*)anObject chosenElementAtIndex:(NSInteger)aIndex;
@end

What is the similar construction for protocols?

like image 831
Michał Zygar Avatar asked Jun 05 '12 08:06

Michał Zygar


2 Answers

Use @protocol for protocols forward declaration:

@protocol Protocol2;
@protocol Protocol1 <NSObject>
-(void)setProtocolDelegate:(id<Protocol2>)delegate;
@end

@protocol Protocol2 <NSObject>
-(void)protocol:(UIViewController<Protocol1>*)anObject chosenElementAtIndex:(NSInteger)aIndex;
@end
like image 73
Vladimir Avatar answered Sep 28 '22 00:09

Vladimir


The problem with your is that you have forward declared protocol with @class keyword. It should be @protocol.

like image 45
Apurv Avatar answered Sep 28 '22 00:09

Apurv