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

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

问题描述

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



编辑:



据我所知,



1)代理实现协议(接口的另一个名称)



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



3)对象可以在代理上调用协议方法



因此,代理将对象与协议连接。 p>

如果我错了,请更正我。



我仍然不明白为什么对象本身不能实施协议?它可以这么简单!

解决方案

协议是一种指定一组您希望类实现的方法的方法如果它想要与你的一个课程一起工作。代表和数据源,如UITableViewDelegate和UITableViewDataSource是确定的协议。



您以这种方式指定协议:

  @protocol MyProtocol< NSObject> 

- (void)aRequiredMethod;

@required
- (void)anotherRequiredMethod;

@optional
- (void)anOptionalMethod;

@end

在@required或之前任何其他说明符是必需的,希望使用协议的类需要实现它们。您也可以通过在@optional说明​​符之后声明一些可选方法。



然后,您可以指定类符合协议(实现所需的方法)在类的界面中:

  @interface MyClass< MyProtocol> 

@end

通常,保持对符合协议的对象的引用使用财产。例如,要跟踪代理:

  @property(nonatomic,weak)id&MyProtocol>代表; 

在这一点上,在你的代码中,你只需要调用你想要调用的方法对象,你保持参考,并实现你的协议,你可以用任何其他方法:

  [self.delegate aRequiredMethod ]; 

要检查对象是否符合协议,您可以调用

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

要检查一个对象实现一个方法,您可以调用

  [self.delegate responsesToSelector:@selector aOptionalMethod]] 

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


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!

EDIT:

As far as I now understand,

1) delegates implement protocols (another name for interfaces)

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

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!

解决方案

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.

You specify a protocol this way:

@protocol MyProtocol <NSObject>

- (void)aRequiredMethod;

@required
- (void)anotherRequiredMethod;

@optional
- (void)anOptionalMethod;

@end

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 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 wether an object conforms to a protocol you can call

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

To check wether an object implements a method you can call

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

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

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

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