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.
我正在尝试扩展NSImageView,以便将拖放责任委派给控制器。一切正常,编译器现在显示有关向类型为id的对象发送消息的警告。为了解决这个问题,我假设我只需要使用协议名称后缀ivar的类型。但是,由于无法找到协议的定义,因此失败了。
#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.
我可能会出错的任何指针?我敢肯定它一定很简单,但我对obj-c很新。
2 个解决方案
#1
31
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
你是在正确的轨道,但你被C编译器挂了,这有点陈旧。编译器窒息,因为协议的定义在您使用时不可用。必须先定义@protocol DragDropImageViewDelegate,然后才能将id
@protocol DragDropImageViewDelegate;
before the @interface (a forward declaration) and leave the @protocol declaration where it is.
在@interface(一个前向声明)之前,将@protocol声明保留在原来的位置。
#2
10
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.
在我看来,协议是声明的一个重要部分,并且往往很短,所以我更喜欢它先行而不是丢失在头文件的底部,但这是一个品味问题。
#1
31
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
你是在正确的轨道,但你被C编译器挂了,这有点陈旧。编译器窒息,因为协议的定义在您使用时不可用。必须先定义@protocol DragDropImageViewDelegate,然后才能将id
@protocol DragDropImageViewDelegate;
before the @interface (a forward declaration) and leave the @protocol declaration where it is.
在@interface(一个前向声明)之前,将@protocol声明保留在原来的位置。
#2
10
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.
在我看来,协议是声明的一个重要部分,并且往往很短,所以我更喜欢它先行而不是丢失在头文件的底部,但这是一个品味问题。