问题描述
我正在创建一个 Reddit 客户端用于学习目的.我需要一个包含常量的文件.我正在考虑在 Reddit-Prefix.pch
文件中导入该文件,以使常量可用于所有文件.这是一种很好的做事方式吗?另外,我做了研究,发现了几种创建常量的方法,但我不知道该使用哪一种:
I am creating a Reddit client for learning purposes. I need to have a file with constants in it. I was thinking about importing the file in the Reddit-Prefix.pch
file to make the constants available to all the files. Is it a good way of doing things? Also, I've done my research and found several methods to create constants, but I don't know which one to use:
#define
宏常量
静态常量
extern const
枚举
那么哪种方式是首选方式?约定是什么?我知道这取决于",但我的问题更具体地说是:每种解决方案的用例是什么?
So which way is the preferred way? What is the convention? I know that "it depends" but my question more specifically is: What are the use cases for each of those solutions?
另外,如果使用extern const
,是否需要导入文件,否则常量在不导入文件的情况下全局可用?
Also, if using extern const
, do I need to import the file, or the constants will be available globally without importing the file?
我可以从逻辑上得出的结论是 enum
是定义自定义错误域之类的最佳选择(我真的是对的吗?).但是其他人呢?
One thing I could logically conclude is that enum
is the best choice when defining something like custom error domains (am I actually right?). But what about the others?
推荐答案
第一个问题是你希望你的常量有什么作用域,其实就是两个问题:
The first question is what scope you want your constants to have, which is really two questions:
- 这些常量是特定于单个类的,还是将它们遍布整个应用程序是否有意义?
- 如果它们是特定于类的,它们是供类的客户使用,还是仅在类内使用?
如果它们特定于单个类并且是内部的,请将它们声明为 .m 文件顶部的 static const
,如下所示:
If they are specific and internal to a single class, declare them as static const
at the top of the .m file, like so:
static NSString *const MyThingNotificationKey = @"MyThingNotificationKey";
如果它们属于单个类但应该被其他类公开/使用,则在标头中将它们声明为 extern
并在 .m 中定义它们:
If they pertain to a single class but should be public/used by other classes, declare them as extern
in the header and define them in the .m:
//.h
extern NSString *const MyThingNotificationKey;
//.m
NSString *const MyThingNotificationKey = @"MyThingNotificationKey";
如果它们应该是全局的,则在标头中声明它们并在相应的模块中定义它们,特别是针对那些常量.
If they should be global, declare them in a header and define them in a corresponding module, specifically for those constants.
您可以将它们混合和匹配用于具有不同全局级别的不同常量,以及根本不属于一起的不同全局常量 - 您可以将它们放在单独的模块中,每个模块都有自己的标题,如果你愿意的话.
You can mix and match these for different constants with different levels of how global you want them to be, and for different global constants that simply don't belong together—you can put them in separate modules, each with its own header, if you want.
旧的答案是宏没有类型信息",但今天的编译器非常聪明地对文字(宏扩展为什么)以及变量进行所有类型检查.
The old answer is "macros don't have type information", but compilers today are pretty smart about doing all the type-checking for literals (what macros expand to) as well as variables.
现代答案是因为调试器不知道您的宏.如果 MyThingNotificationKey
是宏,则不能在调试器命令中说 [myThing addObserver:self forKey:MyThingNotificationKey]
;只有当它是一个变量时,调试器才能知道它.
The modern answer is because the debugger won't know about your macros. You can't say [myThing addObserver:self forKey:MyThingNotificationKey]
in a debugger command if MyThingNotificationKey
is a macro; the debugger can only know about it if it is a variable.
好吧,rmaddy 在评论中击败了我:enum
只能定义整数常量.诸如序列号、位掩码、四字节代码等.
Well, rmaddy beat me to it in the comments: enum
can only define integer constants. Things like serial identifier numbers, bit-masks, four-byte codes, etc.
出于这些目的,enum
非常棒,您绝对应该使用它.(更好的是,使用 NS_ENUM
和 NS_OPTIONS
宏.)其他东西,您必须使用其他东西;enum
除了整数之外什么都不做.
For those purposes, enum
is great and you absolutely should use it. (Even better, use the NS_ENUM
and NS_OPTIONS
macros.) For other things, you must use something else; enum
does not do anything but integers.
我正在考虑将文件导入 Reddit-Prefix.pch 文件中,以使常量可用于所有文件.这是一种很好的做事方式吗?
可能无害,但可能过度.在需要的地方导入常量标头.
Probably harmless, but probably excessive. Import your constants header(s) where you need them.
每个解决方案的用例是什么?
#define
:相当有限.老实说,我不确定是否有充分的理由再将其用于常量.const
:最适合局部常量.此外,您必须将其用于您在标题中声明并正在定义的内容.static const
:最适合文件特定(或类特定)的常量.extern const
:在标头中导出常量时必须使用它.#define
: Pretty limited. I'm honestly not sure there's a good reason to use this for constants anymore.const
: Best for local constants. Also, you have to use this for one you declared in a header and are now defining.static const
: Best for file-specific (or class-specific) constants.extern const
: You must use this when exporting a constant in a header.
另外,如果使用extern const
,是否需要导入文件,否则常量在不导入文件的情况下全局可用?
您需要在使用它的每个文件中或在前缀标题中导入该文件.
You need to import the file, either in each file where you use it or in the prefix header.
这篇关于在 Objective-C 中创建常量的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!