目标C中的非正式协议?

编程入门 行业动态 更新时间:2024-10-25 00:25:44
本文介绍了目标C中的非正式协议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想知道是否有人可以解释目标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 @protocols 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中的非正式协议?

本文发布于:2023-11-06 07:02:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563009.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:协议   目标

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!