协议与代表之间的区别? [英] Difference between protocol and delegates?

查看:120
本文介绍了协议与代表之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

协议委托之间有什么区别?

我们如何在协议类中声明变量

How can we declare variables in a protocol class?

推荐答案

使用( @protocol Objective-C中的语法)用于声明类采用(声明将使用此协议)将实现的一组方法。这意味着您可以在代码中指定,只要实现特定协议,就不用关心使用哪个类。这可以在Objective-C中完成,如下所示:

A protocol, declared with the (@protocol syntax in Objective-C) is used to declare a set of methods that a class "adopts" (declares that it will use this protocol) will implement. This means that you can specify in your code that, "you don't care which class is used as long as it implements a particular protocol". This can be done in Objective-C as follows:

id&MyProtocol> instanceOfClassThatImplementsMyProtocol;

如果您在代码中说明这一点,那么任何符合协议的类MyProtocol可以在变量 instanceOfClassThatImplementsMyProtocol 中使用。这意味着使用此变量的代码知道它可以使用与该特定变量在MyProtocol 中定义的任何方法,而不管它是什么类。这是避免继承设计模式的一个很好的方式,避免紧密耦合。

If you state this in your code, then any class that "conforms" to the protocol MyProtocol can be used in the variable instanceOfClassThatImplementsMyProtocol. This means that the code that uses this variable knows that it can use whichever methods are defined in MyProtocol with this particular variable, regardless of what class it is. This is a great way of avoiding the inheritance design pattern, and avoids tight coupling.

代表是使用协议的语言特性。 委派设计模式是设计您的代码以在必要时使用协议的一种方式。在Cocoa框架中,委托设计模式用于指定符合特定协议的类的实例。该特定协议指定了委托类应该实现的方法,以在给定事件中执行特定的操作。使用委托的类知道它的委托与协议一致,所以它知道它可以在给定的时间调用实现的方法。这种设计模式是一个很好的解耦类的方法,因为它使得一个委托实例交换为另一个变得非常简单 - 所有程序员都要做的是确保替换实例或类符合必要的协议(即它实现方案在协议中指定)!

Delegates are a use of the language feature of protocols. The delegation design pattern is a way of designing your code to use protocols where necessary. In the Cocoa frameworks, the delegate design pattern is used to specify an instance of a class which conforms to a particular protocol. This particular protocol specifies methods that the delegate class should implement to perform specific actions at given events. The class that uses the delegate knows that its delegate coforms to the protocol, so it knows that it can call the implemented methods at given times. This design pattern is a great way of decoupling the classes, because it makes it really easy to exchange one delegate instance for another - all the programmer has to do is ensure that the replacement instance or class conforms to the necessary protocol (i.e. it implements the methods specified in the protocol)!

协议和代理不仅限于Objective-C和Mac / iOS开发,而是Objective-C语言和Apple框架大量使用这种令人敬畏的语言功能和设计模式。

Protocols and delegates are not restricted only to Objective-C and Mac/iOS development, but the Objective-C language and the Apple frameworks make heavy use of this awesome language feature and design pattern.

编辑:

这是一个例子。在Cocoa Touch的UIKit框架中,有一个 UITextFieldDelegate 协议。该协议定义了一系列方法,这些方法是实现UITextField 实例的委托的类。换句话说,如果要将委托分配给一个 UITextField (使用委托属性),最好确保这个类符合 UITextFieldDelegate 。事实上,因为 UITextField 的委托属性被定义为:

Here's an example. In the UIKit framework of Cocoa Touch, there is a UITextFieldDelegate protocol. This protocol defines a series of methods that classes which are delegates of a UITextField instance should implement. In other words, if you want to assign a delegate to a UITextField (using the delegate property), you'd better make sure that this class conforms to UITextFieldDelegate. In fact, because the delegate property of UITextField is defined as:

@property(nonatomic,assign)id< UITextFieldDelegate>委托

然后,如果您为没有实现协议的类分配一个类,那么编译器会给出警告。这真的很有用你必须说明一个类实现一个协议,并且说它是,你让其他类知道他们可以以特定的方式与你的类进行交互。因此,如果您将 MyTextFieldDelegateClass 的实例分配给 UITextField 委托属性,则 UITextField 知道,它可以调用您的 MyTextFieldDelegateClass 的某些特定方法(与文本输入,选择等相关)。它知道这一点,因为MyTextFieldDelegateClass 表示它将实现 UITextFieldDelegate 协议。

Then the compiler will give warnings if you assign a class to it that doesn't implement the protocol. This is really useful. You have to state that a class implements a protocol, and in saying that it does, you're letting other classes know that they can interact in a particular way with your class. So, if you assign an instance of MyTextFieldDelegateClass to the delegate property of UITextField, the UITextField knows that it can call some particular methods (related to text entry, selection etc.) of your MyTextFieldDelegateClass. It knows this because MyTextFieldDelegateClass has said that it will implement the UITextFieldDelegate protocol.

最终,这一切都导致您项目代码中的灵活性和适应性更强大,我相信您在使用此技术后即将意识到! :)

Ultimately, this all leads to much greater flexibility and adaptability in your project's code, which I'm sure you'll soon realise after using this technology! :)

这篇关于协议与代表之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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