gpt4 book ai didi

objective-c - Objective C 中的协议(protocol)命名

转载 作者:太空狗 更新时间:2023-10-30 03:10:27 26 4
gpt4 key购买 nike

我正在为 Objective-C 中的命名协议(protocol)而苦恼。例如:

我有一个名为 Command 的协议(protocol)。我有一个实现 Command 的抽象类,它是我的具体命令的基类。

我相信可以同时调用协议(protocol)和基类“命令”,但这会造成混淆,如果我需要在实现中引用协议(protocol),将会导致导入冲突。我也明白在 Objective C 中,使用前缀来表示协议(protocol)是错误的形式。一些示例使用添加到末尾的“ing”,但在这种情况下这是没有意义的。调用抽象类“CommandBase”似乎也是错误的。

那我应该怎么命名呢?

最佳答案

我建议,在您的情况下,将您的协议(protocol)和基类命名为相同的东西不一定不好,因为您的类是协议(protocol)的主要表达(例如使用 NSObject) .

来自 Apple 的 Coding Guidelines for Cocoa: Code Naming Basics :

Some protocols group a number of unrelated methods (rather than create several separate small protocols). These protocols tend to be associated with a class that is the principal expression of the protocol. In these cases, the convention is to give the protocol the same name as the class. An example of this sort of protocol is the NSObject protocol. This protocol groups methods that you can use to query any object about its position in the class hierarchy, to make it invoke specific methods, and to increment or decrement its reference count. Because the NSObject class provides the primary expression of these methods, the protocol is named after the class.

关于objective-c - Objective C 中的协议(protocol)命名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7483813/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com