类常量
我有几个 obj-c 类,每个类都需要一些在 switch 语句中使用的常量。
我曾尝试使用#define 预处理器指令在 .m 文件中定义这些数字常量。所有这些常量都以“kCell”开头。这似乎工作得很好,但 Xcode 的代码感知向我展示了每个以“kCell”为前缀的常量,无论我在项目中的哪个位置。 #define
常量是否具有全局作用域?如果是这样,定义纯本地类常量的最佳方法是什么,这些常量不会与其他类中类似命名的常量进行分类?
I have several obj-c classes, each of which require a number of constants that are used in switch statements.
I had tried defining these numerical constants in the .m file using the #define
preprocessor instruction. All these constants begin with 'kCell'. This seems to work well but Xcode's code sense is presenting me with every 'kCell' prefixed constant no matter where I am in the project. Do #define
constants have global scope? If so, what is the best way to define purely local class constants that won't class with similarly named constants in other classes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
拥有自己的常量文件,例如 MyConstants。
在 MyConstants.h 中声明所有常量:
并在 MyConstants.m 中定义它们
通过将常量保存在单独的文件中,您可以轻松跟踪它们并更改它们的值。
这是定义纯常量的最佳方式。这也将避免重复的密钥。
您只需要做的就是将此类导入到其他类中:
并立即使用这些键:
Have your own constant file like MyConstants.
In MyConstants.h declare all your constants:
and in MyConstants.m define them
By keeping constants in separate file, it will be easy for you to trace them and change their value.
This is the best way to define purely constants. And this will avoid duplicate keys also.
Only you need to do is import this class in you other classes:
and use these keys straight away:
我通常发现枚举最适合用于开关:
不幸的是,xCode 并不将其代码感知(代码完成、自动完成)限制为任何特定文件。它试图找出可以从代码的哪些区域访问哪些常量,但我注意到它并不是 100% 准确。
我建议不要以相同的前缀开始它们。例如,如果您有两种不同类型的表格单元格,kCustomCell 和 kOtherCell 可能是命名常量的更好方法。
I generally find that enums are best used for switches:
Unfortunately xCode doesn't restrict it's code sense (code completion, auto-complete) to any specific file. It tries to figure out which constants are accessible from which areas of your code but I've noticed it's not 100% accurate.
I would suggest not starting them all with the same prefix. For example, if you have two different types of table cells, kCustomCell and kOtherCell might be better ways to name you constants.
#define
常量仅存在于.m
文件中。如果常量是整数,您也可以在枚举中定义它们:
The
#define
constants only exist in the.m
file.If the constants are integers you could also define them in an
enum
:这似乎工作得很好,但 Xcode 的代码感知向我展示了每个以“kCell”为前缀的常量,无论我在项目中的哪个位置。 #define 常量具有全局作用域吗?如果是这样,定义纯本地类常量的最佳方法是什么,这些常量不会与其他类中类似命名的常量进行类比?
Xcode Code Sense 和运行时可用性并不相同。 Xcode 还提供了选择器名称和其他在运行时不可见的项目内容。
.m 文件中定义的所有内容在运行时都是本地的。
另外看看这个问题:
Objective-C 中的常量
This seems to work well but Xcode's code sense is presenting me with every 'kCell' prefixed constant no matter where I am in the project. Do #define constants have global scope? If so, what is the best way to define purely local class constants that won't class with similarly named constants in other classes?
Xcode Code Sense and Runtime availability are not identical. Xcode also offers selectorNames and other stuff of items that are not visible at runtime.
Everything defined in a .m file is local at runtime.
Also have a look at this question:
Constants in Objective-C