xcode 类常量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2377368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:10:06  来源:igfitidea点击:

Class Constants

objective-ciphonexcodec-preprocessor

提问by Garry Pettet

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 #definepreprocessor 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 #defineconstants 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?

我曾尝试使用#define预处理器指令在 .m 文件中定义这些数字常量。所有这些常量都以“kCell”开头。这似乎工作得很好,但无论我在项目中的哪个位置,Xcode 的代码意识都向我展示了每个带有“kCell”前缀的常量。做#define常量具有全局范围是什么?如果是这样,定义纯本地类常量的最佳方法是什么,而这些常量不会与其他类中的类似命名常量类同?

回答by Manjunath

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

回答by ImHuntingWabbits

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.

不幸的是,xCode 并没有将它的代码意义(代码完成、自动完成)限制到任何特定文件。它试图找出可以从代码的哪些区域访问哪些常量,但我注意到它不是 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 可能是命名常量的更好方法。

回答by kennytm

The #defineconstants only exist in the .mfile.

#define常数只存在于.m文件。

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

如果常量是整数,您还可以在一个中定义它们enum

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

回答by maxbareis

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 的代码意识都向我展示了每个带有“kCell”前缀的常量。#define 常量是否具有全局作用域?如果是这样,定义纯本地类常量的最佳方法是什么,而这些常量不会与其他类中的类似命名常量类同?

Xcode Code Sense and Runtime availability are not identical. Xcode also offers selectorNames and other stuff of items that are not visible at runtime.

Xcode Code Sense 和运行时可用性并不相同。Xcode 还提供了 selectorNames 和其他在运行时不可见的项目。

Everything defined in a .m file is local at runtime.

.m 文件中定义的所有内容在运行时都是本地的。

Also have a look at this question: Constants in Objective-C

也看看这个问题: Constants in Objective-C