objective-c - 普查员类型转换

标签 objective-c c

尝试将自己的枚举器转换为地址簿值时出现错误:

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/

相关文章:

objective-c - 游戏设计 : Is a plug-in the best solution for this Problem?(Cocoa、Objective-C)

ios - [[NSRunLoop currentRunLoop] runMode :* beforeDate:*]; 中的 BAD EXC

c - Linux,从条码扫描仪读取

ios - 调用 [PFFFacebookUtils initializeFacebook] 时解析崩溃 - semaphore_wait_trap

objective-c - 比较 Objective C 中的字符串

ios - 使白色标签在明亮的 ImageView 顶部可见

c - AVR 使用 struct 或 #define 定义引脚

c++ - 无法运行任何c/c++程序

c - 如何通过网络完全控制终端?

c - OpenMP 任务和 while 循环