协议和委托到底是什么,它们在 IOS 中是如何使用的? [英] What exactly are protocols and delegates and how are they used in IOS?

查看:21
本文介绍了协议和委托到底是什么,它们在 IOS 中是如何使用的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的对委托和协议的概念感到困惑.它们是否等同于 Java 中的接口和适配器类?它们是如何工作的?到目前为止,我读过的所有资源都没有帮助.委托是一种简单而强大的模式,其中程序中的一个对象代表另一个对象或与另一个对象协调.委托对象保持对另一个对象(委托)的引用,并在适当的时间发送消息给它."我不知道这是什么意思.有人可以解释一下它们是什么并举一个简单的例子吗?提前致谢!

I'm really confused about the concept of delegates and protocols. Are they equivalent of interfaces and adapter classes in Java? How do they work? None of the resources I've read were helpful so far. "Delegation is a simple and powerful pattern in which one object in a program acts on behalf of, or in coordination with, another object. The delegating object keeps a reference to the other object—the delegate—and at the appropriate time sends a message to it." I have no idea what this means. Can someone please explain what they are and give a simple example? Thanks in advance!

据我所知,

1) 委托实现协议(接口的另一个名称)

1) delegates implement protocols (another name for interfaces)

2) 对象注册一个委托(实现协议)

2) object registers a delegate (that implements a protocol)

3) 对象可以在委托上调用协议方法

3) object can call protocol methods on the delegate

因此,委托将对象与协议连接起来.

Therefore, a delegate is connecting the object with the protocol.

如果我错了,请纠正我.

Please correct me if I'm wrong.

我还是不明白为什么对象本身不能实现协议?本来可以轻松得多!

I still don't understand why the object itself can't implement a protocol? It could've been so much easier!

推荐答案

协议是一种指定一组方法的方法,如果它想与你的一个类一起工作,你希望一个类实现.委托和数据源,如 UITableViewDelegate 和 UITableViewDataSource 确实是协议.

Protocols are a way to specify a set of methods you want a class to implement if it wants to work with one of your classes. Delegates and Data Sources like UITableViewDelegate and UITableViewDataSource are protocols indeed.

您可以这样指定协议:

@protocol MyProtocol <NSObject>

- (void)aRequiredMethod;

@required
- (void)anotherRequiredMethod;

@optional
- (void)anOptionalMethod;

@end

在@required 之后或任何其他说明符之前声明的方法是必需的,并且想要使用您的协议的类需要实现所有这些方法.您还可以通过在 @optional 说明符之后声明一些可选方法来声明它们.

Methods declared after the @required or before any other specifier are required and the classes that want to use your protocol need to implement all of them. You can also declare some optional methods by declaring them after the @optional specifier.

然后您可以在类的接口中指定一个类符合"协议(实现所需的方法):

You then can specify that a class "conforms" to a protocol (implements the required methods) in the interface of the class:

@interface MyClass <MyProtocol>

@end

您通常使用属性保留对符合协议的对象的引用.例如,要跟踪委托:

You usually keep a reference to an object conforming to a protocol using a property. For example, to keep track of a delegate:

@property (nonatomic, weak) id<MyProtocol> delegate;

此时,在您的代码中,您只需要调用您想要调用的对象上的方法,该对象将保持引用并像使用任何其他方法一样实现您的协议:

At this point, in your code, you just have to call the method you want to call on the object that you're keeping reference of and that implements your protocol as you would with any other method:

[self.delegate aRequiredMethod];

检查一个对象是否符合你可以调用的协议

To check whether an object conforms to a protocol you can call

[self.delegate conformsToProtocol:@protocol(MyProtocol)]

检查一个对象是否实现了你可以调用的方法

To check whether an object implements a method you can call

[self.delegate respondsToSelector:@selector(anOptionalMethod)]

有关更多信息,请查看 Apple 指南 使用协议.

For more information, check the Apple's guide Working With Protocols.

这篇关于协议和委托到底是什么,它们在 IOS 中是如何使用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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