Objective-C 中的非正式协议?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2010058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Informal Protocol In objective-C?
提问by itsaboutcode
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.
我想知道是否有人可以解释什么是 Objective C 中的非正式协议?我试图在苹果文档和其他一些书籍上理解它,但我的头脑仍在旋转,所以如果有人能用例子来解释,我将非常感激。
Thanks.
谢谢。
回答by bbum
An informal protocolwas, 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).
一个非正式的协议是,作为Jonnathan说,通常一个类别宣布NSObject中没有对应的实现(最常见的-有这并提供NSObject的虚拟实现罕见的一个)。
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):
从 10.6(以及在 iPhone SDK 中)开始,不再使用此模式。具体来说,在 10.5(及之前)中声明如下:
@interface NSObject(NSApplicationNotifications)
- (void)applicationWillFinishLaunching:(NSNotification *)notification;
...
@interface NSObject(NSApplicationDelegate)
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
...
Is now declared as:
现在声明为:
@protocol NSApplicationDelegate <NSObject>
@optional
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
...
- (void)applicationWillFinishLaunching:(NSNotification *)notification;
...
That is, informal protocols are now declared as @protocols with a bunch of @optionalmethods.
也就是说,非正式协议现在被声明为@protocol带有一堆@optional方法的s 。
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).
在任何情况下,非正式协议都是方法声明的集合,您可以在其中选择实现方法来改变行为。通常,但并非总是如此,方法实现是在委托上下文中提供的(例如,表视图的数据源必须实现一些必需的方法,并且可以选择实现一些其他方法)。
回答by Daniel Yankowsky
One of the common examples given of informal protocols is to define callbacks. Suppose you are using a library that lets you download something in the background. This library lets you register a callback object to be called when complete.
非正式协议的常见示例之一是定义回调。假设您正在使用一个允许您在后台下载某些内容的库。这个库让你注册一个回调对象,当完成时被调用。
- (void)download:(NSURL*)url whenComplete:(id)callback
When the download is complete, it will call a particular method on your callback object:
下载完成后,它将调用回调对象上的特定方法:
- (void)downloadComplete:(NSURL*)url
Of course, there is no guarantee that your callback object actually implements this method. Informal protocols provide trivial implementations of these methods on NSObject, using a category. As a result, all objects in the system will respond to the downloadComplete:method, though they will do nothing in response to that method by default. Classes that override the downloadComplete:method can provide more useful functionality.
当然,不能保证您的回调对象确实实现了此方法。非正式协议NSObject使用类别提供了这些方法的简单实现。结果,系统中的所有对象都会响应该downloadComplete:方法,尽管默认情况下它们不会响应该方法。覆盖该downloadComplete:方法的类可以提供更有用的功能。
So far, you can accomplish the same thing with a formal protocol. However, informal protocols allow you to have optional methods. A class that implements a formal protocol must provide an implementation for every method in the protocol. A class implementing an informal protocol can omit implementation for any method - it has already inherited an implementation from NSObject.
到目前为止,您可以使用正式协议完成相同的事情。但是,非正式协议允许您拥有可选的方法。实现正式协议的类必须为协议中的每个方法提供一个实现。实现非正式协议的类可以省略任何方法的实现——它已经从NSObject.
Since Objective-C 2.0, formal protocols can contain optional methods. In addition, Apple might be moving away from informal protocols for new APIs - UIAccelerometerDelegateis a formal protocol.
从 Objective-C 2.0 开始,正式协议可以包含可选方法。此外,Apple 可能会放弃新 API 的非正式协议 -UIAccelerometerDelegate是正式协议。
回答by Durai Amuthan.H
Informal protocolsare a way to add optional methodsto an object by means of category.
非正式协议是一种通过类别向对象添加可选方法的方法。
So one doubt may arise
所以可能会产生一个疑问
Will it become informal protocol if there are any optional methods on protocol itself?
如果协议本身有任何可选方法,它会成为非正式协议吗?
The answer is no.
答案是不。
If the methods are declared in protocol and it is said to be conforming to a class without any usage of categorythen it's formal protocol.
如果方法是在协议中声明的,并且据说它符合一个类而不使用任何类别,那么它就是正式的协议。
Note:
笔记:
Optional methods in protocol were introduced in objective c 2.0 so before that the purpose was achieved through informal protocol I.e by means of category.
协议中的可选方法是在目标 c 2.0 中引入的,因此在此之前,该目的是通过非正式协议即通过类别实现的。
Category:
类别:
It is a language level feature meant to be alternative for sub classing aka inheritance.
它是一种语言级别的功能,旨在替代子类又名继承。
I hope it sheds some lime light on this..
我希望它能对此有所启发..
回答by Amol Manwatkar
We define an informal protocolby grouping the methods in a category declaration,
我们informal protocol通过在类别声明中对方法进行分组来定义一个,
@interface NSObject ( MyXMLSupport )
- initFromXMLRepresentation:(NSXMLElement *)XMLElement;
- (NSXMLElement *)XMLRepresentation;
@end
Informal protocols are typically declared as categories of the NSObjectclass, Since that broadly associates
the method names with any class that inherits from NSObject.
Informal protocols 通常被声明为类的NSObject类别,因为这将方法名称与从NSObject.
Because all classes inherit from the root class,
the methods aren't restricted to any part of the inheritance hierarchy. (It would also be possible to declare
an informal protocolas a category of another class to limit it to a certain branch of the inheritance hierarchy,
but there is little reason to do so).
因为所有类都从根类继承,所以方法不限于继承层次结构的任何部分。(也可以将 an 声明informal protocol为另一个类的类别以将其限制为继承层次结构的某个分支,但几乎没有理由这样做)。
When used to declare a protocol, a category interface doesn't have a corresponding implementation. Instead, classes that implement the protocol declare the methods again in their own interface files and define them along with other methods in their implementation files.
当用于声明协议时,类别接口没有相应的实现。相反,实现协议的类在它们自己的接口文件中再次声明这些方法,并在它们的实现文件中将它们与其他方法一起定义。
回答by Jonathan Sterling
All an informal protocol is is a category on some class (often NSObject) that states the interface for the protocol. AppKit uses this a lot for its delegation.
所有的非正式协议都是某个类(通常NSObject)上的一个类别,它声明了协议的接口。AppKit 在其委托中经常使用它。
A subclass that you write can implement these methods. The difference between this and a formal protocol is that formal protocols are declared using the @protocol ... @endnotation. There is no checking whether a class implementsa given informal protocol.
您编写的子类可以实现这些方法。这与正式协议之间的区别在于,正式协议是使用@protocol ... @end符号声明的。没有检查类是否实现了给定的非正式协议。
I almost always use formal protocols, but I suppose an informal protocol is useful if you want to provide default behavior (just provide an implementation for your category that can be overridden).
我几乎总是使用正式协议,但我认为如果您想提供默认行为(只需为您的类别提供一个可以被覆盖的实现),非正式协议会很有用。
回答by itsaboutcode
Based on "Jonathan Sterling" answer, can i say the following code represent informal protocol?
基于“Jonathan Sterling”的回答,我可以说以下代码代表非正式协议吗?
Apple documentation:
苹果文档:
"When used to declare a protocol, a category interface doesn't have a corresponding implementation. Instead, classes that implement the protocol declare the methods again in their own interface files and define them along with other methods in their implementation files."
“当用于声明协议时,类别接口没有相应的实现。相反,实现该协议的类在自己的接口文件中再次声明方法,并在其实现文件中与其他方法一起定义它们。”
#import <Foundation/Foundation.h>
@interface Cat1 : NSObject {
}
- (void) simpleMethod;
@end
@implementation Cat1
- (void) simpleMethod
{
NSLog(@"Simple Method");
}
@end
@interface Cat1 (Cat2)
- (void) addingMoreMethods;
@end
@interface MYClass : Cat1
@end
@implementation MYClass
- (void) addingMoreMethods
{
NSLog(@"Testing!");
}
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
MYClass *myclass = [[MYClass alloc] init];
[myclass addingMoreMethods];
[myclass release];
[pool drain];
return 0;
}
回答by Pirmin Braun
an informal protocol defines which methods an object must understand. This is called "conforming to a protocol". Conforming to a protocol is independant from the class hierarchy. When declaring a pointer to hold the reference to an object you may define to which protocols this object should conform. If you write code that assigns an object which doesn't conform to all the required protocols, you'll get a warning at compile time. Informal protocols help you to rely on a set of methods that an object understands. You don't have to invoke isKindOfClass: or respondsTo: in your code to check wether objects passed in will be suitable for your processing. Protocols are sort of aspect oriented programming.
一个非正式的协议定义了一个对象必须理解哪些方法。这称为“符合协议”。遵守协议与类层次结构无关。当声明一个指针来保存对一个对象的引用时,你可以定义这个对象应该遵守哪些协议。如果您编写的代码分配的对象不符合所有必需的协议,您将在编译时收到警告。非正式协议帮助您依赖对象理解的一组方法。您不必在代码中调用 isKindOfClass: 或 RespondsTo: 来检查传入的对象是否适合您的处理。协议是一种面向方面的编程。

