Solving circular protocol declarations

2019-03-02 03:01发布

问题:

I am trying to solve this, there are similar questions but none are fit for my issue

I have .h file with a a @protocol. The @interface needs to have that @protocol definition. But the @protocol needs the class's definition. Circular.

How can this be solved?

@protocol myProtocol

-(void)setupMyClass:(MyClass *)class;

@end

@inteface MyClass

@property (weak, nonatomic) id<myProtocol> delegate;

@end

If I use different files each needs to import the other, so that doesn't solve it

回答1:

Forward-declare the class:

@class MyClass;    // Forward declaration

@protocol MyProtocol
...
@end

@interface MyClass
...
@end