类常量

发布于 2024-08-23 22:49:17 字数 247 浏览 1 评论 0原文

我有几个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

那支青花 2024-08-30 22:49:17

拥有自己的常量文件,例如 MyConstants。

在 MyConstants.h 中声明所有常量:

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

并在 MyConstants.m 中定义它们

NSString* const kCellTitleKey = @"CellTitle";

通过将常量保存在单独的文件中,您可以轻松跟踪它们并更改它们的值。

这是定义纯常量的最佳方式。这也将避免重复的密钥。

您只需要做的就是将此类导入到其他类中:

#import "MyConstants.h"

并立即使用这些键:

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

Have your own constant file like MyConstants.

In MyConstants.h declare all your constants:

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

and in MyConstants.m define them

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];
葬心 2024-08-30 22:49:17

我通常发现枚举最适合用于开关:

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

不幸的是,xCode 并不将其代码感知(代码完成、自动完成)限制为任何特定文件。它试图找出可以从代码的哪些区域访问哪些常量,但我注意到它并不是 100% 准确。

我建议不要以相同的前缀开始它们。例如,如果您有两种不同类型的表格单元格,kCustomCell 和 kOtherCell 可能是命名常量的更好方法。

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. 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.

夏夜暖风 2024-08-30 22:49:17

#define 常量仅存在于 .m 文件中。

如果常量是整数,您也可以在枚举中定义它们:

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

The #define constants only exist in the .m file.

If the constants are integers you could also define them in an enum:

enum {
  kCellSomething = 123,
  kCellAnotherThing = 456,
  ...
};
你げ笑在眉眼 2024-08-30 22:49:17

这似乎工作得很好,但 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文