Objective-C中正式和非正式协议之间的区别是什么? [英] What's the difference between formal and informal protocols in Objective-C?

查看:190
本文介绍了Objective-C中正式和非正式协议之间的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Objective-C中正式和非正式协议之间有什么区别?

What's the difference between formal and informal protocols in Objective-C?

推荐答案

来自官方文档

正式和非正式协议

有两种协议,
正式和非正式:

There are two varieties of protocol, formal and informal:


  • 非正式协议是NSObject上的一个类别,隐含地
    使几乎所有对象都采用
    协议。 (一个类别是一个
    语言特性,它使你能够在没有
    子类化的情况下向一个类添加
    。)非正式协议中
    方法的实现是
    可选的。在调用方法之前,
    调用对象检查
    目标对象是否实现了
    。在Objective-C 2.0中引入了
    的可选协议方法之前,
    非正式协议对于
    是必不可少的,就像Foundation和AppKit类
    实现委托一样。

  • An informal protocol is a category on NSObject, which implicitly makes almost all objects adopters of the protocol. (A category is a language feature that enables you to add methods to a class without subclassing it.) Implementation of the methods in an informal protocol is optional. Before invoking a method, the calling object checks to see whether the target object implements it. Until optional protocol methods were introduced in Objective-C 2.0, informal protocols were essential to the way Foundation and AppKit classes implemented delegation.

正式协议声明了客户类
预计要实现的方法列表。正式的
协议有自己的声明,
采用和类型检查语法。
您可以使用@required和@optional
关键字指定需要
实现的方法或可选的
。子类继承其祖先采用的正式
协议。
正式协议也可以采用其他
协议。

A formal protocol declares a list of methods that client classes are expected to implement. Formal protocols have their own declaration, adoption, and type-checking syntax. You can designate methods whose implementation is required or optional with the @required and @optional keywords. Subclasses inherit formal protocols adopted by their ancestors. A formal protocol can also adopt other protocols.

正式协议是对
Objective-C语言。

Formal protocols are an extension to the Objective-C language.

这篇关于Objective-C中正式和非正式协议之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆