协议和代表们
可能的重复:
通知、委托和协议之间有什么区别?
我是目标 C 的新手。想要用几句话来理解协议
和委托
的概念。就像在什么情况下我应该考虑声明一个协议,然后在我的类中作为委托使用它或让其他类使用它。
为什么我不能让我的类完成相同的工作而不是协议?
感谢您抽出时间。
Possible Duplicate:
What is the difference between Notifications, Delegates, and Protocols?
I am new in Objective C. Would like to understand the concept of Protocols
and Delegates
in a few words. Like under what situation should I consider declaring a Protocol
, and then consume it in my classes as delegates or let other classes consume it.
Instead of Protocols
why can't I make my classes do the same job?
Thanks for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
协议就像契约一样,您的类必须实现所有 @required 的方法,而 @Optional 则不是必需的。
实现(必需)方法。
delegate 是(应该是)对实现给定协议的类的弱引用。
建议您在委托属性上使用以下属性:
请注意,您不保留委托。你(弱)引用是因为你不想进入保留圈(A 保留 B,B 保留 A)。
我希望我的解释能有所帮助。
Protocols are like contracts, Your class must implement all methods that are @required, @optional are on the other hand not required.
implements the (Required) methods.
delegate is (should be) a weak reference to the class that implements the given protocol.
You are recommended to use the following attribute on your delegateproperty:
Notice that you don't retain your delegate. You you (weak) reference because you dont want to get into a retain circle (A retains B, and B retains A).
I hope my explanation helped a bit.