I have a .h file that defines a few hundred constants. Let's assume this to be one of them:
我有一个.h文件,定义了几百个常量。我们假设这是其中之一:
#define KDSomeItem 1
I know that the Objective-C runtime API can be used to retrieve a list of instance variable names: like detailed in this question: How do I list all fields of an object in Objective-C? I also know that the getter [object valueForKey:theName]
can be used to access the ivars as found in an earlier question of mine: How to access a property/variable using a String holding its name
我知道Objective-C运行时API可用于检索实例变量名称列表:如本问题中详述:如何列出Objective-C中对象的所有字段?我也知道getter [object valueForKey:theName]可用于访问我之前的一个问题中的ivars:如何使用保存其名称的String访问属性/变量
My question is can something simmilar be done with constants? That is can I:
我的问题是可以用常数完成一些事情吗?那是我可以:
a) get a list of all constants programmatically
a)以编程方式获取所有常量的列表
b) access a constant if I have a string holding its name
b)如果我有一个保存其名称的字符串,则访问常量
e.g. if I had a String like this NSString * theName = @"KDSomeItem";
could I evaluate my constant using this string?
例如如果我有一个像这样的字符串NSString * theName = @“KDSomeItem”;我可以用这个字符串来评估我的常量吗?
2 个解决方案
#1
1
You would not be able to do this: unlike instance variables, #define
-d constants do not leave a trace after the preprocessor is done with them. They leave no metadata behind - all instances of KDSomeItem
in the body of your program will be replaced with 1
even before the Objective C compiler proper gets to analyze your code.
你将无法做到这一点:与实例变量不同,#define-d常量在预处理器完成之后不会留下痕迹。它们不会留下任何元数据 - 即使在Objective C编译器正确分析代码之前,程序体中的所有KDSomeItem实例都将替换为1。
If you need the constant names to be available at run time, you would need to build all the necessary metadata yourself. In order to do that, you may want to look into "stringizing" operator of the preprocessor:
如果您需要在运行时提供常量名称,则需要自己构建所有必需的元数据。为此,您可能需要查看预处理器的“stringizing”运算符:
#define STR(X) #X
This macro can be applied to a preprocessor constant, and produce a C string literal with its name:
此宏可以应用于预处理器常量,并生成一个C字符串文字,其名称为:
const char *nameOfKDSomeItem = STR(KDSomeItem); // same as "KDSomeItem"
#2
0
Nope. Your constant is a preprocessor macro. It is textually substituted into your source code where you use it.
不。您的常量是预处理器宏。它以文本方式替换为您使用它的源代码。
#1
1
You would not be able to do this: unlike instance variables, #define
-d constants do not leave a trace after the preprocessor is done with them. They leave no metadata behind - all instances of KDSomeItem
in the body of your program will be replaced with 1
even before the Objective C compiler proper gets to analyze your code.
你将无法做到这一点:与实例变量不同,#define-d常量在预处理器完成之后不会留下痕迹。它们不会留下任何元数据 - 即使在Objective C编译器正确分析代码之前,程序体中的所有KDSomeItem实例都将替换为1。
If you need the constant names to be available at run time, you would need to build all the necessary metadata yourself. In order to do that, you may want to look into "stringizing" operator of the preprocessor:
如果您需要在运行时提供常量名称,则需要自己构建所有必需的元数据。为此,您可能需要查看预处理器的“stringizing”运算符:
#define STR(X) #X
This macro can be applied to a preprocessor constant, and produce a C string literal with its name:
此宏可以应用于预处理器常量,并生成一个C字符串文字,其名称为:
const char *nameOfKDSomeItem = STR(KDSomeItem); // same as "KDSomeItem"
#2
0
Nope. Your constant is a preprocessor macro. It is textually substituted into your source code where you use it.
不。您的常量是预处理器宏。它以文本方式替换为您使用它的源代码。