为什么要制作一个全选可选消息协议? [英] Why would I make an all-optional message protocol?

查看:100
本文介绍了为什么要制作一个全选可选消息协议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个项目编写一个Cocoa API,该API接受一个委托。我提出的协议声明所有的方法作为可选,但为什么我会这样做,而不是只记录在一个头文件中的委托方法,并以一个简单的id作为参数?

I'm writing a Cocoa API for a project and the API takes a delegate. The protocol that I came up with declares all the methods as optional, but why would I do that instead of just documenting the delegate methods in a header file and taking a plain id as a parameter?

推荐答案

为了您的用户的利益。如果对象接受符合某种协议的委托,并且它们通过别的东西,编译器可以告诉他们。如果你使用id并使用类别作为委托方法接口,那么这是不可能的。

For the benefit of your users. If the object takes delegates conforming to some protocol and they pass something else in, the compiler can tell them. That isn't possible if you take an id and use a category as the delegate method interface.

这篇关于为什么要制作一个全选可选消息协议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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