在Objective-C中创建常量的最佳方法是什么 [英] What is the best way to create constants in Objective-C

查看:117
本文介绍了在Objective-C中创建常量的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建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
  • const
  • static const
  • extern const
  • enum
  • #define macro
  • const
  • static const
  • extern const
  • enum

那么哪种方式是首选方式?约定是什么?我知道取决于",但是我的问题更具体是:这些解决方案的用例是什么?

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];否则,不能在调试器命令中输入[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_ENUMNS_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文件中,以使常量可用于所有文件.这是做事的好方法吗?

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?

可能无害,但可能过度.将常量头文件导入需要的地方.

Probably harmless, but probably excessive. Import your constants header(s) where you need them.

每种解决方案的用例是什么?

What are the use cases for each of those solutions?

  • #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,我是否需要导入文件,或者常量将在不导入文件的情况下全局可用?

      Also, if using extern const, do I need to import the file, or the constants will be available globally without importing the file?

      您需要在使用文件的每个文件中或在前缀标头中导入文件.

      You need to import the file, either in each file where you use it or in the prefix header.

      这篇关于在Objective-C中创建常量的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆