문제

I am trying to create several protocols, and most of them have references to other ones. But I get error during the build process.

I give an example:

#import <Foundation/Foundation.h>

@protocol DataChildDelegate <NSObject>

@property(nonatomic) id<DataParentDelegate> parent;

@end

@protocol DataParentDelegate <NSObject>

@property(nonatomic) id<DataChildDelegate> firstChild;
@property(nonatomic) id<DataChildDelegate> lastChild;

@end

I tried to divide DataChildDelegate in two parts like this:

#import <Foundation/Foundation.h>

@protocol DataChildDelegate <NSObject>
@end

@protocol DataParentDelegate <NSObject>

@property(nonatomic) id<DataChildDelegate> firstChild;
@property(nonatomic) id<DataChildDelegate> lastChild;

@end

@protocol DataChildDelegate <NSObject>

@property(nonatomic) id<DataParentDelegate> parent;

@end

But this time I get a warning.

Is there any more suitable way to handle this problem?

Thanks

도움이 되었습니까?

해결책

You should use a forward declaration of the protocol DataChildDelegate prior to DataParentDelegate so that the compiler can trust that it exists.

For example:

#import <Foundation/Foundation.h>

@protocol DataChildDelegate;     /*Forward declaration of DataChildDelegate */

@protocol DataParentDelegate <NSObject>

@property(nonatomic) id<DataChildDelegate> firstChild;
@property(nonatomic) id<DataChildDelegate> lastChild;

@end

@protocol DataChildDelegate <NSObject>

@property(nonatomic) id<DataParentDelegate> parent;

@end
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top