What's the difference between formal and informal protocols in Objective-C?
Objective-C中正式协议和非正式协议的区别是什么?
3 个解决方案
#1
56
从官方文档
Formal and Informal Protocols
正式和非正式的协议
There are two varieties of protocol, formal and informal:
协议有两种,正式的和非正式的:
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.
非正式协议是NSObject上的一个类别,它隐式地使协议的几乎所有对象采纳者。(category是一种语言特性,它允许您向类添加方法,而无需对类进行子类化。)在非正式协议中实现方法是可选的。在调用方法之前,调用对象检查目标对象是否实现了它。在Objective-C 2.0中引入了可选的协议方法之前,非正式的协议是基础和AppKit类实现委托的关键。
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.
正式协议声明了客户端类希望实现的方法列表。正式协议有自己的声明、采用和类型检查语法。您可以使用@required和@optional关键字指定其实现是必需的或可选的方法。子类继承其祖先所采用的正式协议。正式协议也可以采用其他协议。
Formal protocols are an extension to the Objective-C language.
形式协议是Objective-C语言的扩展。
#2
12
Informal Protocol : Category
(Implementations are Optional)
非正式协议:类别(实现是可选的)
Formal Protocol : Extension
(Implementations are Optional and required)
正式协议:扩展(实现是可选和必需的)
#3
2
The Objective-C language provides a way to formally declare a list of methods (including declared properties) as a protocol. Formal protocols are supported by the language and the runtime system. For example, the compiler can check for types based on protocols, and objects can introspect at runtime to report whether or not they conform to a protocol.
Objective-C语言提供了一种将方法列表(包括声明的属性)正式声明为协议的方法。语言和运行时系统支持正式的协议。例如,编译器可以根据协议检查类型,并且对象可以在运行时内省,以报告它们是否符合协议。
#1
56
从官方文档
Formal and Informal Protocols
正式和非正式的协议
There are two varieties of protocol, formal and informal:
协议有两种,正式的和非正式的:
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.
非正式协议是NSObject上的一个类别,它隐式地使协议的几乎所有对象采纳者。(category是一种语言特性,它允许您向类添加方法,而无需对类进行子类化。)在非正式协议中实现方法是可选的。在调用方法之前,调用对象检查目标对象是否实现了它。在Objective-C 2.0中引入了可选的协议方法之前,非正式的协议是基础和AppKit类实现委托的关键。
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.
正式协议声明了客户端类希望实现的方法列表。正式协议有自己的声明、采用和类型检查语法。您可以使用@required和@optional关键字指定其实现是必需的或可选的方法。子类继承其祖先所采用的正式协议。正式协议也可以采用其他协议。
Formal protocols are an extension to the Objective-C language.
形式协议是Objective-C语言的扩展。
#2
12
Informal Protocol : Category
(Implementations are Optional)
非正式协议:类别(实现是可选的)
Formal Protocol : Extension
(Implementations are Optional and required)
正式协议:扩展(实现是可选和必需的)
#3
2
The Objective-C language provides a way to formally declare a list of methods (including declared properties) as a protocol. Formal protocols are supported by the language and the runtime system. For example, the compiler can check for types based on protocols, and objects can introspect at runtime to report whether or not they conform to a protocol.
Objective-C语言提供了一种将方法列表(包括声明的属性)正式声明为协议的方法。语言和运行时系统支持正式的协议。例如,编译器可以根据协议检查类型,并且对象可以在运行时内省,以报告它们是否符合协议。