尝试将自己的枚举器转换为地址簿值时出现错误:
typedef enum {
kACTextFirstName = kABPersonFirstNameProperty, // error: expression is not an integer constant expression
kACTextLastName = (int)kABPersonLastNameProperty, // error: expression is not an integer constant expression
} ACFieldType;
如何解决问题?
谢谢。
我需要使用 ABAddressBook 的框架常量值(例如 kABPersonLastNameProperty 或 kABPersonFirstNameProperty)来初始化我的枚举。
最佳答案
在 C 中(与 C++ 不同),声明为 const
的对象,即使它是用常量表达式初始化的,也不能用作常量。
您没有费心向我们展示 kABPersonFirstNameProperty
的声明,但我猜它的声明类似于:
const int kABPersonFirstNameProperty = 42;
如果您需要使用名称kABPersonFirstNameProperty
作为常量表达式,您可以将其声明为宏:
#define kABPersonFirstNameProperty 42
或作为枚举常量:
enum { kABPersonFirstNameProperty = 42 };
请注意,枚举 hack 只允许您声明 int
类型的常量。
同样适用于 kABPersonLastNameProperty
。
(为什么要将其中一个转换为 int
,而不是另一个?)
如果这不能回答您的问题,那是因为您没有向我们提供足够的信息。
关于objective-c - 普查员类型转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7405327/