Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining and using protocols in objective-c

Tags:

I'm trying to extend NSImageView so I can delegate the drag/drop responsibility to the controller. It all works fine with the one problem that the compiler is now displaying warnings about sending messages to objects with type id. To solve this I assumed I would simply have to suffix the ivar's type with the name of the protocol. However, this fails miserably with the message that it cannot find the definition for the protocol.

#import <Cocoa/Cocoa.h>   @interface DragDropImageView : NSImageView {     id <DragDropImageViewDelegate> _delegate; }  @property (readwrite, retain) id <DragDropImageViewDelegate> delegate;  @end  @protocol DragDropImageViewDelegate  @optional  - (NSDragOperation)dragDropImageView:(DragDropImageView *)ddiv validateDrop:(id     <NSDraggingInfo>)info; - (BOOL)dragDropImageView:(DragDropImageView *)ddiv acceptDrop:(id <NSDraggingInfo>)info; - (void)concludeDragOperation:(id <NSDraggingInfo>)sender;    @end 

Any pointers where I might be going wrong? I'm sure it must be something simple, but I'm quite new to obj-c.

like image 741
Tricky Avatar asked Jun 10 '09 14:06

Tricky


People also ask

What are protocols Objective-C?

Objective-C allows you to define protocols, which declare the methods expected to be used for a particular situation. This chapter describes the syntax to define a formal protocol, and explains how to mark a class interface as conforming to a protocol, which means that the class must implement the required methods.

How do you conform a protocol in Objective-C?

Objective-C Language Protocols Conforming to Protocols It is also possible for a class to conform to multiple protocols, by separating them with comma. Like when conforming to a single protocol, the class must implement each required method of each protocols, and each optional method you choose to implement.

What is protocol and delegate in Objective-C?

Protocols and Delegates are two related but different concept: A Protocol is a interface a class can conforms to, meaning that class implements the listed methods. A Delegate is typically an anonymous object that conforms to a protocol. The application of Delegate called Delegation is a design pattern.


2 Answers

You're on the right track but you're getting hung up by the C compiler, which is a little archaic. The compiler is choking because the definition of the protocol is not available at the time you use it. @protocol DragDropImageViewDelegate must be defined before you can use id< DragDropImageViewDelegate> as a type. You can move the @protocol definition before the usage (i.e. before your @interface), or add a

@protocol DragDropImageViewDelegate; 

before the @interface (a forward declaration) and leave the @protocol declaration where it is.

like image 136
Barry Wark Avatar answered Sep 28 '22 10:09

Barry Wark


As a general rule, I define the protocol first, preceeded by

@class DragDropImageView; 

But you can do the reverse and preceed with:

@protocol DragDropImageViewDelegate; 

To my mind, the protocol is an important part of the declaration, and tends to be quite short, so I prefer it to go first rather than be lost at the bottom of the header file, but its a matter of taste.

like image 39
Peter N Lewis Avatar answered Sep 28 '22 09:09

Peter N Lewis