macos 在 Objective-C 中定义和使用协议

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/975888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:12:55  来源:igfitidea点击:

Defining and using protocols in objective-c

objective-cmacoscocoa-touchcocoansdragginginfo

提问by Tricky

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 很陌生。

回答by Barry Wark

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 DragDropImageViewDelegatemust 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< DragDropImageViewDelegate>用作类型。您可以在使用之前(即在您的@interface 之前)移动@protocol 定义,或者添加一个

@protocol DragDropImageViewDelegate;

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

在@interface(前向声明)之前,并将@protocol 声明保留在原处。

回答by Peter N Lewis

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.

在我看来,协议是声明的重要组成部分,而且往往很短,所以我更喜欢它首先出现而不是丢失在头文件的底部,但它是一个品味问题。