尝试将自己的枚举器转换为通讯簿值时出错:
typedef enum {
kACTextFirstName = kABPersonFirstNameProperty, // error: expression is not an integer constant expression
kACTextLastName = (int)kABPersonLastNameProperty, // error: expression is not an integer constant expression
} ACFieldType;
Run Code Online (Sandbox Code Playgroud)
如何解决问题?
谢谢.
我需要使用ABAddressBook的框架const值来初始化我的枚举,例如kABPersonLastNameProperty或kABPersonFirstNameProperty.
在C语言中(与C++不同),声明的对象const
,即使用常量表达式初始化,也不能用作常量.
你没有向我们展示声明kABPersonFirstNameProperty
,但我猜它的声明如下:
const int kABPersonFirstNameProperty = 42;
Run Code Online (Sandbox Code Playgroud)
如果需要将名称kABPersonFirstNameProperty
用作常量表达式,可以将其声明为宏:
#define kABPersonFirstNameProperty 42
Run Code Online (Sandbox Code Playgroud)
或者作为枚举常量:
enum { kABPersonFirstNameProperty = 42 };
Run Code Online (Sandbox Code Playgroud)
请注意,枚举黑客只允许您声明类型的常量int
.
同样地kABPersonLastNameProperty
.
(你为什么要把其中一个投入int
,而不是另一个?)
如果这不能回答你的问题,那是因为你没有给我们足够的信息.