Objective C 中的协议命名
我正在努力解决 Objective-C 中的命名协议问题。例如:
我有一个名为 Command 的协议。 我有一个实现 Command 的抽象类,它是我的具体命令的基类。
我相信可以同时调用协议和基类“Command”,但这很令人困惑,并且如果我需要在实现中引用协议,则会导致导入冲突。我还了解到,在 Objective C 中,使用前缀来表示协议是不好的形式。有些示例使用添加到末尾的“ing”,但在本例中这是没有意义的。调用抽象类“CommandBase”似乎也是错误的。
那么我应该如何命名它们呢?
I'm struggling with naming protocols in Objective-C. For example:
I have a protocol called Command.
I have an abstract class that implements Command that is a base class for my concrete Commands.
I believe it is possible to call both the protocol and the base class 'Command' but this is confusing and will cause import clashes if I need to reference the protocol in an implementation. I also understand that in Objective C, using a prefix to denote a protocol is bad form. Some examples use 'ing' added to the end, but in this instance that makes no sense. Calling the abstract class 'CommandBase' seems wrong as well.
So how should I name them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议,在您的情况下,将协议和基类命名为相同的名称不一定是坏事,因为您的类是协议的主要表达式(例如使用
NSObject
)。来自苹果的 Cocoa 编码指南:代码命名基础:
I would suggest that in your case it is not necessarily bad to name your protocol and the base class the same thing, as your class is the principal expression of the protocol (such as with
NSObject
).From Apple's Coding Guidelines for Cocoa: Code Naming Basics:
Apple 的 Cocoa 编码指南 代码命名基础知识。
作者指出:
All covered in Apple's Coding Guidelines For Cocoa in the section Code Naming Basics.
The author states:
如果你会看到 uitableview、NSUrlconnection 的预定义协议,那么你将得到协议的名称,就像 UItabaleviewDelegate 和 NSUrlconnectionDelegate 一样。 ......
然后你可以很容易地理解哪个委托属于哪个类
所以你可以使用你的classnameDelegate作为协议名称....谢谢
if you will see predefine protocal of uitableview, NSUrlconnection then u will get the name of protocal just like UItabaleviewDelegate and NSUrlconnectionDelegate. ........
Then you can undertand easy which delegate is belong from which class
So u can use your classnameDelegate as protocal name ....thanks