Objective C 中的协议命名

发布于 2024-12-06 01:55:23 字数 283 浏览 0 评论 0原文

我正在努力解决 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南风几经秋 2024-12-13 01:55:24

我建议,在您的情况下,将协议和基类命名为相同的名称不一定是坏事,因为您的类是协议的主要表达式(例如使用 NSObject)。

来自苹果的 Cocoa 编码指南:代码命名基础

一些协议将许多不相关的方法分组(而不是创建
几个独立的小协议)。这些协议往往是
与作为主要表达式的类相关联
协议。在这些情况下,惯例是给予协议
与班级同名。此类协议的一个示例是
NSObject 协议。该协议对可用于
查询任何对象有关其在类层次结构中的位置,以使其
调用特定方法,并增加或减少其引用
数数。因为 NSObject 类提供了主要表达式
这些方法,协议以类命名。

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:

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.

樱花落人离去 2024-12-13 01:55:24

Apple 的 Cocoa 编码指南 代码命名基础知识。

作者指出:

协议应该根据它们如何分组行为来命名:

大多数协议将相关方法分组,但不与任何协议相关联。
特别是类。这种类型的协议应该命名为
协议不会与类混淆。一个常见的约定是使用
动名词(“...ing”)形式:

NSLocking - 好。

NSLock - 差(看起来像是一个类的名称)。

一些协议将许多不相关的方法分组(而不是创建
几个独立的小协议)。这些协议往往是
与作为主要表达式的类相关联
协议。在这些情况下,惯例是给予协议
与类的名称相同。

此类协议的一个示例是 NSObject 协议。这
协议组方法可用于查询任何对象的相关信息
在类层次结构中的位置,使其调用特定方法,
并增加或减少其引用计数。因为
NSObject 类提供了这些方法的主要表达方式,
协议以类命名。

All covered in Apple's Coding Guidelines For Cocoa in the section Code Naming Basics.

The author states:

Protocols should be named according to how they group behaviors:

Most protocols group related methods that aren’t associated with any
class in particular. This type of protocol should be named so that the
protocol won’t be confused with a class. A common convention is to use
a gerund (“...ing”) form:

NSLocking - Good.

NSLock - Poor (seems like a name for a class).

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.

み青杉依旧 2024-12-13 01:55:24

如果你会看到 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文