博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Objective-C protocol 和 delegate
阅读量:5812 次
发布时间:2019-06-18

本文共 7949 字,大约阅读时间需要 26 分钟。

概述

  1. protocol类似C++中的抽象类和Java中的interface。分为Formal protocol和informal protocol两种,其中在Objective C 2.0之前,informal protocol是Cocoa中实现delegate的重要基础。因为Objective C 2.0引入了@optional和@required关键字。
  2. delegate是一种常用的设计模式,而不是Objective C或者Cocoa的feature,也没有任何关键字是给delegate用的。

protocol

Formal protocol 声明了一个客户类需要实现的方法列表,它有自己独立的declaration、adoption和类型检查语法。这个方法列表中的方法可以使用@required @optional 标注,以表示该方法是否是客户类必须要实现的方法。如果父类实现了某个formal protocol,那子类可以完全继承其实现。Formal protocol可以继承其他的Formal protocol。

Informal protocol是NSObject的category(见),并没有独立的声明、采用和类型检查的语法,跟普通的类和方法没什么区别。在informal protocol中的方法其实现不是必须的。在invoke某个方法前,calling object需要检查target object是否实现了这个方法。

下面是一个formal protocol的例子: 

// declare 3 protocols in a.h file@protocol MSFormalProtocol 
@required- (void) foo;@end@protocol MSFormalProtocol2
@optional- (void) bar;@end@protocol MSFormalProtocol3
@end //define the interface which adopt these 3 protocols@interface MSTestFormalProtocol : NSObject
@end//but we do not implement these required method@implementation MSTestFormalProtocol@end

 从上面的例子可以看到,协议的adoption 语法是<Protocol1, [Protocol1,...]>。如果要声明一个变量,它的要求是adopt某个/几个protocol,那么其声明语法可以如下所示:

id 
*ptr;

从NSTableViewDataSource 的声明也可以看到这样的例子。 

@protocol NSTableViewDataSource 
@optional#pragma mark -#pragma mark ***** Required Methods (unless bindings are used) *****- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView;/* This method is required for the "Cell Based" TableView, and is optional for the "View Based" TableView. If implemented in the latter case, the value will be set to the view at a given row/column if the view responds to -setObjectValue: (such as NSControl and NSTableCellView). */- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row;#pragma mark -#pragma mark ***** Optional Methods *****/* NOTE: This method is not called for the View Based TableView. */- (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row;/* Sorting support This is the indication that sorting needs to be done. Typically the data source will sort its data, reload, and adjust selections.*/- (void)tableView:(NSTableView *)tableView sortDescriptorsDidChange:(NSArray *)oldDescriptors;/* Dragging Source Support - Required for multi-image dragging. Implement this method to allow the table to be an NSDraggingSource that supports multiple item dragging. Return a custom object that implements NSPasteboardWriting (or simply use NSPasteboardItem). If this method is implemented, then tableView:writeRowsWithIndexes:toPasteboard: will not be called. */- (id
)tableView:(NSTableView *)tableView pasteboardWriterForRow:(NSInteger)row NS_AVAILABLE_MAC(10_7);/* Dragging Source Support - Optional. Implement this method to know when the dragging session is about to begin and to potentially modify the dragging session.'rowIndexes' are the row indexes being dragged, excluding rows that were not dragged due to tableView:pasteboardWriterForRow: returning nil. The order will directly match the pasteboard writer array used to begin the dragging session with [NSView beginDraggingSessionWithItems:event:source]. Hence, the order is deterministic, and can be used in -tableView:acceptDrop:row:dropOperation: when enumerating the NSDraggingInfo's pasteboard classes. */- (void)tableView:(NSTableView *)tableView draggingSession:(NSDraggingSession *)session willBeginAtPoint:(NSPoint)screenPoint forRowIndexes:(NSIndexSet *)rowIndexes NS_AVAILABLE_MAC(10_7);/* Dragging Source Support - Optional. Implement this method to know when the dragging session has ended. This delegate method can be used to know when the dragging source operation ended at a specific location, such as the trash (by checking for an operation of NSDragOperationDelete). */- (void)tableView:(NSTableView *)tableView draggingSession:(NSDraggingSession *)session endedAtPoint:(NSPoint)screenPoint operation:(NSDragOperation)operation NS_AVAILABLE_MAC(10_7);/* Dragging Destination Support - Required for multi-image dragging. Implement this method to allow the table to update dragging items as they are dragged over the view. Typically this will involve calling [draggingInfo enumerateDraggingItemsWithOptions:forView:classes:searchOptions:usingBlock:] and setting the draggingItem's imageComponentsProvider to a proper image based on the content. For View Based TableViews, one can use NSTableCellView's -draggingImageComponents. For cell based TableViews, use NSCell's draggingImageComponentsWithFrame:inView:. */- (void)tableView:(NSTableView *)tableView updateDraggingItemsForDrag:(id
)draggingInfo NS_AVAILABLE_MAC(10_7);/* Dragging Source Support - Optional for single-image dragging. Implement this method to support single-image dragging. Use the more modern tableView:pasteboardWriterForRow: to support multi-image dragging. This method is called after it has been determined that a drag should begin, but before the drag has been started. To refuse the drag, return NO. To start a drag, return YES and place the drag data onto the pasteboard (data, owner, etc...). The drag image and other drag related information will be set up and provided by the table view once this call returns with YES. 'rowIndexes' contains the row indexes that will be participating in the drag. */- (BOOL)tableView:(NSTableView *)tableView writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard *)pboard;/* Dragging Destination Support - This method is used by NSTableView to determine a valid drop target. Based on the mouse position, the table view will suggest a proposed drop 'row' and 'dropOperation'. This method must return a value that indicates which NSDragOperation the data source will perform. The data source may "re-target" a drop, if desired, by calling setDropRow:dropOperation: and returning something other than NSDragOperationNone. One may choose to re-target for various reasons (eg. for better visual feedback when inserting into a sorted position).*/- (NSDragOperation)tableView:(NSTableView *)tableView validateDrop:(id
)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)dropOperation;/* Dragging Destination Support - This method is called when the mouse is released over an NSTableView that previously decided to allow a drop via the validateDrop method. The data source should incorporate the data from the dragging pasteboard at this time. 'row' and 'dropOperation' contain the values previously set in the validateDrop: method.*/- (BOOL)tableView:(NSTableView *)tableView acceptDrop:(id
)info row:(NSInteger)row dropOperation:(NSTableViewDropOperation)dropOperation;/* Dragging Destination Support - NSTableView data source objects can support file promised drags by adding NSFilesPromisePboardType to the pasteboard in tableView:writeRowsWithIndexes:toPasteboard:. NSTableView implements -namesOfPromisedFilesDroppedAtDestination: to return the results of this data source method. This method should returns an array of filenames for the created files (filenames only, not full paths). The URL represents the drop location. For more information on file promise dragging, see documentation on the NSDraggingSource protocol and -namesOfPromisedFilesDroppedAtDestination:.*/- (NSArray *)tableView:(NSTableView *)tableView namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropDestination forDraggedRowsWithIndexes:(NSIndexSet *)indexSet;@end

 

语法:

由于Objective-C 在编译时不检查方法是否存在,所以如果我们要求某个类满足某个协议,由于不是强类型的,所以编译器不检查我们是否正确遵照这个协议实现了,从而导致错误。比如,NSTableView的datasource要求实现方法:

- (int) numberOfRowsInTableView:(NSTableView *)aTableView;

如果不小心写错了,比如少写或者多写了一个字母,那编译时没有任何错误,但是运行起来的时候,不work。

 

转载于:https://www.cnblogs.com/whyandinside/archive/2013/02/28/2937217.html

你可能感兴趣的文章
Django 多表联合查询
查看>>
Freebsd系统故障导致系统不能正常启动的恢复数据方法[图]
查看>>
logging模块学习:basicConfig配置文件
查看>>
Golang 使用 Beego 与 Mgo 开发的示例程序
查看>>
DHCP服务器数据备份以及还原
查看>>
ntpdate时间同步
查看>>
Asp.Net MVC 插件化开发简化方案
查看>>
LH乱码转换
查看>>
+++++++子域授权与编译安装(一)
查看>>
编程修养(七)
查看>>
asp.net怎样在URL中使用中文、空格、特殊字符
查看>>
ISA2006实战系列之二:实战ISA三种客户端部署方案(下)
查看>>
Linux后门入侵检测工具,附bash漏洞最终解决方法
查看>>
ASA5585-S20测试方案
查看>>
利用for循环打印实心棱形和空心棱形
查看>>
路由器发布服务器
查看>>
实现跨交换机VLAN间的通信
查看>>
oracle import & export 操作相关脚本
查看>>
LVS集群的体系结构,构建强壮的体系结构里负载均衡层、真实服务器层、后端共享存储层都是相辅相成...
查看>>
VTK读取raw格式的数据
查看>>