Objective-C 中如何命名常量?

发布于 2024-09-30 01:49:21 字数 308 浏览 1 评论 0原文

Objective-C 中常量的命名约定是什么(或者最广泛使用的命名方式)?

extern 常量是否有不同的标准?

我见过的一些款式:

NSString* const kPreferenceFirstRun = @"FirstRun";

// Replace "XY" by a prefix representing your company, project or module
NSString* const XYPreferenceFirstRun = @"FirstRun"; 

What's the naming convention for constants in Objective-C (or most widely used way to name them)?

Is there a different criteria for extern constants?

Some styles I have seen:

NSString* const kPreferenceFirstRun = @"FirstRun";

// Replace "XY" by a prefix representing your company, project or module
NSString* const XYPreferenceFirstRun = @"FirstRun"; 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

弥枳 2024-10-07 01:49:21

经过一番谷歌搜索后,我发现了 Cocoa 官方编码指南

总结一下:

  • 以两个或三个字母 前缀 全部
  • 大写 其余部分采用大驼峰形式
  • extern 常量的标准相同

我同意 itaiferber 的观点k 前缀样式更清晰,对于自动完成也更有用。了解这种风格是否比官方指南更受欢迎将会很有趣。

After a bit of googling I've found the official coding guidelines for Cocoa.

To sum up:

  • Start with a two- or three-letter prefix in ALL-CAPS
  • Rest in UpperCamelCase
  • Same criteria for extern constants

I agree with itaiferber that the k prefix style is clearer and also much more useful for autocompletion. It would be interesting to know if this style is more popular than the official guidelines.

黎夕旧梦 2024-10-07 01:49:21

在我看来,最佳实践是以大写字母命名常量。
但可可核心开发人员似乎不同意我的观点))他们使用 CamelCase 来表示常量

it's seems to me, the best practice is to name constants in upper-case.
but cocoa-core developers don't seem to share my opinion)) they use CamelCase for constants

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