问题描述
我想知道是否有人可以解释目标C中的非正式协议?我尝试在Apple文档和其他一些书上理解它,但是我的头还在转动,所以如果有人可以举例说明,我将非常感激.
I was wondering if someone can explain what is informal protocols in Objective C? I try to understand it on apple documentation and some other books but my head is still spinning so i will really appreciate that if someone can explain with example.
谢谢.
推荐答案
非正式协议通常是在NSObject上声明的类别,没有相应的实现(通常-确实在NSObject上提供虚拟实现的稀有版本.)
An informal protocol was, as Jonnathan said, typically a category declared on NSObject with no corresponding implementation (most often -- there was the rare one that did provide dummy implementations on NSObject).
从10.6(和iPhone SDK)开始,不再使用此模式.具体来说,在10.5(及更低版本)中声明如下:
As of 10.6 (and in the iPhone SDK), this pattern is no longer used. Specifically, what was declared as follows in 10.5 (and prior):
@interface NSObject(NSApplicationNotifications)
- (void)applicationWillFinishLaunching:(NSNotification *)notification;
...
@interface NSObject(NSApplicationDelegate)
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
...
现在声明为:
@protocol NSApplicationDelegate <NSObject>
@optional
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
...
- (void)applicationWillFinishLaunching:(NSNotification *)notification;
...
也就是说,现在非正式协议已通过一系列@optional
方法声明为@protocol
.
That is, informal protocols are now declared as @protocol
s with a bunch of @optional
methods.
在任何情况下,非正式协议都是方法声明的集合,您可以在其中选择性地实现更改行为的方法.通常,但并非总是如此,方法实现是在委托的上下文中提供的(例如,表视图的数据源必须实现少数必需的方法,并且可以有选择地实现一些其他方法).
In any case, an informal protocol is a collection of method declarations whereby you can optionally implement the methods to change behavior. Typically, but not always, the method implementations are provided in the context of delegation (a table view's data source must implement a handful of required methods and may optionally implement some additional methods, for example).
这篇关于目标C中的非正式协议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!