katit
katit

Reputation: 17915

Declaring delegate protocol in the same file

I have following header:

@protocol AttachmentsViewDelegate <NSObject>

@required
- (void)spaceRequestedWithSize:(CGSize)size sender:(AttachmentsView*)sender;

@end

@interface AttachmentsView : UIView

@property id<AttachmentsViewDelegate> delegate;
@property NSArray *attachments;

- (void)takePicture;
- (void)deletePictures;

@end

Doesn't work because inside @protocol I reference to AttachmentsView and it's not declared yet.

If I move @protocol below @interface - I have another problem, because delegate property doesn't know about @protocol.

I know I can use id, UIView for types, but to keep it "strongly typed", what can I do? I really prefer not to break it down into 2 files. Any other options/suggestions?

Upvotes: 1

Views: 409

Answers (2)

ppalancica
ppalancica

Reputation: 4277

just write:

@class AttachmentsView;

on top of the file.

In case you wish to declare the class first, and then the protocol, write first:

@protocol AttachmentsViewDelegate;

on top of the file.

Upvotes: 1

jlehr
jlehr

Reputation: 15597

Use @class to forward-declare AttachmentsView like so:

@class AttachmentsView;

@protocol AttachmentsViewDelegate <NSObject>

// Protocol method declarations...

@end

Alternatively, use @protocol to forward-declare the protocol:

@protocol AttachementsViewDelegate

@interface AttachmentsView : UIView

// Ivar, method, and property declarations...

@property id<AttachmentsViewDelegate> delegate;

@end

Upvotes: 2

Related Questions