如何在Objective-C中命名常量?

时间:2021-02-26 20:56:51

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

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

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"; 

2 个解决方案

#1


38  

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

经过一番谷歌搜索后,我找到了Cocoa的官方编码指南。

To sum up:

总结一下:

  • Start with a two- or three-letter prefix in ALL-CAPS
  • 在ALL-CAPS中以两个或三个字母的前缀开头
  • Rest in UpperCamelCase
  • 在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.

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

#2


5  

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

在我看来,最好的做法是以大写字母命名常量。但可可核心开发人员似乎并不赞同我的意见))他们使用CamelCase作为常量

#1


38  

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

经过一番谷歌搜索后,我找到了Cocoa的官方编码指南。

To sum up:

总结一下:

  • Start with a two- or three-letter prefix in ALL-CAPS
  • 在ALL-CAPS中以两个或三个字母的前缀开头
  • Rest in UpperCamelCase
  • 在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.

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

#2


5  

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

在我看来,最好的做法是以大写字母命名常量。但可可核心开发人员似乎并不赞同我的意见))他们使用CamelCase作为常量