简体   繁体   English

类常量

[英]Class Constants

I have several obj-c classes, each of which require a number of constants that are used in switch statements. 我有几个obj-c类,每个类都需要一些在switch语句中使用的常量。

I had tried defining these numerical constants in the .m file using the #define preprocessor instruction. 我曾尝试使用#define预处理器指令在.m文件中定义这些数值常量。 All these constants begin with 'kCell'. 所有这些常数都以'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. 这似乎运作良好,但无论我在项目中的哪个位置,Xcode的代码意义都会向我呈现每个'kCell'前缀常量。 Do #define constants have global scope? #define常量是否具有全局范围? If so, what is the best way to define purely local class constants that won't class with similarly named constants in other classes? 如果是这样,那么定义纯本地类常量的最佳方法是什么,这些常量不会在其他类中使用类似命名的常量进行分类?

Have your own constant file like MyConstants. 拥有自己的常量文件,如MyConstants。

In MyConstants.h declare all your constants: 在MyConstants.h中声明所有常量:

static const int kRedisplayTileCount = 5 ;
extern  NSString* const kCellTitleKey;

and in MyConstants.m define them 并在MyConstants.m中定义它们

NSString* const kCellTitleKey = @"CellTitle";

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: 只有您需要做的是在其他类中导入此类:

#import "MyConstants.h"

and use these keys straight away: 并立即使用这些密钥:

obj = [[NSUserDefaults standardUserDefaults] integerForKey: kCellTitleKey];

I generally find that enums are best used for switches: 我通常发现枚举最适合用于交换机:

typedef enum {
  kCellConstantOne = 1,
  kCellConstantTwo = 2, //...
} kCellConstants;

/* later */
- (void)foo:(kCellConstant)constant {
  switch (constant) {
    case kCellConstantOne:
      //do something
      break;
    case kCellConstantTwo:
      //do something else
      break;
  }
}

Unfortunately xCode doesn't restrict it's code sense (code completion, auto-complete) to any specific file. 不幸的是,xCode不会将代码检测(代码完成,自动完成)限制为任何特定文件。 It tries to figure out which constants are accessible from which areas of your code but I've noticed it's not 100% accurate. 它试图找出哪些常量可以从代码的哪些区域访问,但我注意到它不是100%准确。

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. 例如,如果您有两种不同类型的表格单元格,kCustomCell和kOtherCell可能是更好的方法来命名常量。

The #define constants only exist in the .m file. #define常量仅存在于.m文件中。

If the constants are integers you could also define them in an enum : 如果常量是整数,您还可以在enum定义它们:

enum {
  kCellSomething = 123,
  kCellAnotherThing = 456,
  ...
};

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. 这似乎运作良好,但无论我在项目中的哪个位置,Xcode的代码意义都会向我呈现每个'kCell'前缀常量。 Do #define constants have global scope? #define常量是否具有全局范围? 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代码感知和运行时可用性不相同。 Xcode also offers selectorNames and other stuff of items that are not visible at runtime. Xcode还提供了selectorNames和其他在运行时不可见的项目。

Everything defined in a .m file is local at runtime. .m文件中定义的所有内容在运行时都是本地的。

Also have a look at this question: Constants in Objective-C 还要看看这个问题: Objective-C中的常量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM