protocol
和 delegate
有什么区别?
和,
我们如何在protocol class
中声明variables
?
使用(Objective-C 中的@protocol
语法)声明的协议用于声明类的一组方法adopts"(声明它将使用此协议)将实现.这意味着您可以在代码中指定只要它实现特定协议,您就不会关心使用哪个类".这可以在 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<我的协议>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 分配一个委托(使用 delegate
属性),你最好确保这个类符合 UITextFieldDelegate.其实是因为UITextField的delegate属性定义为:
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, weak) id<UITextFieldDelegate>委托
如果你给它分配了一个没有实现协议的类,编译器会给出警告.这真的很有用.你必须声明一个类实现了一个协议,并且说它实现了,你让其他类知道他们可以以特定的方式与你的类交互.因此,如果您将 MyTextFieldDelegateClass 的实例分配给 UITextField 的 delegate
属性,则 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! :)
这篇关于协议和代表之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!