什么是一个枚举,其值显示为字符串类型? [英] What is the type of an enum whose values appear to be strings?

查看:175
本文介绍了什么是一个枚举,其值显示为字符串类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正与苹果公司 ScriptingBridge 框架内工作,并已经产生了iTunes的一个头文件,其中包含几个枚举取值像这样的:

I am working with Apple's ScriptingBridge framework, and have generated a header file for iTunes that contains several enums like this:

typedef enum {
    iTunesESrcLibrary = 'kLib',
    iTunesESrcIPod = 'kPod',
    iTunesESrcAudioCD = 'kACD',
    iTunesESrcMP3CD = 'kMCD',
    iTunesESrcDevice = 'kDev',
    iTunesESrcRadioTuner = 'kTun',
    iTunesESrcSharedLibrary = 'kShd',
    iTunesESrcUnknown = 'kUnk'
} iTunesESrc;

我的理解是,枚举值必须是整数样,但这个定义似乎违反这条规则。此外,它好像治疗这些枚举值作为整数(在 NS predicate ,例如)没有做正确的事。

My understanding was that enum values had to be integer-like, but this definition seems to violate that rule. Furthermore, it seems as though treating these enum values as integers (in an NSPredicate, for example) doesn't do the right thing.

我添加了枚举声明的上方用一个空功能的C文件,它使用编译的i686-苹果darwin9-GCC-4.0.1 。因此,尽管这类枚举取值可能不符合C标准(如Parappa下面指出的),他们至少被编译的部分通过GCC类型。

I added the enum declaration above to a C file with an empty main function, and it compiled using i686-apple-darwin9-gcc-4.0.1. So, while these kinds of enums may not conform to the C standard (as Parappa points out below), they are at least being compiled to some type by gcc.

那么,什么是类型,我如何使用它,比如,在格式字符串?

So, what is that type, and how can I use it, for instance, in a format string?

推荐答案

C99,TC3写着:

C99, TC3 reads:

6.4.4.4§2:

6.4.4.4 §2:

这是整型字符常量是的一个或多个字节用单引号,如X字符的序列。 [...]

An integer character constant is a sequence of one or more multibyte characters enclosed in single-quotes, as in 'x'. [...]

6.4.4.4§10:

6.4.4.4 §10:

这是整型字符常量的类型为int。包含映射到单字节执行字符一个字符的整型字符常量的值是PTED作为整数映射的字符间$ P $重新presentation的数值。包含多个字符(例如,'AB'),或包含字符或转义序列不映射到单字节执行字符,整数字符常量的值是实现定义的。如果一个整型字符常量包含一个字符或转义序列,它的价值在于结果时具有char类型的值的对象是单个字符或转义序列转换为int类型的人。

An integer character constant has type int. The value of an integer character constant containing a single character that maps to a single-byte execution character is the numerical value of the representation of the mapped character interpreted as an integer. The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined. If an integer character constant contains a single character or escape sequence, its value is the one that results when an object with type char whose value is that of the single character or escape sequence is converted to type int.

在大多数实现中,它的安全使用多达4个单字节字符的整数字符常量。实际值可能虽然不同系统之间的不同(字节序?)。

In most implementations, it's safe to use integer character constants of up to 4 one-byte characters. The actual value might differ between different systems (endianness?) though.


这实际上已经在ANSI-C89标准定义,部分3.1.3.4:

This is actually already defined in the ANSI-C89 standard, section 3.1.3.4:

这是整数字符常数是一个或多个的序列
  用单引号,如'X'或'AB'多字节字符。 [...]

An integer character constant is a sequence of one or more multibyte characters enclosed in single-quotes, as in 'x' or 'ab'. [...]

这是整型字符常量的类型为int。的价值
  包含映射单个字符整型字符常量
  进入基本执行字符集的成员是数字
  PTED作为一个映射的字符间$ P $重新presentation的价值
  整数。含有较多的整数字符常量的值
  多个字符,或包含字符或转义序列不
  再在基本执行字符集psented $ P $,是
  实现定义。特别是,在一个实施中
  char类型具有相同的值范围为符号的字符,高位
  单字符整型字符常量的位位置
  视为一个符号位。

An integer character constant has type int. The value of an integer character constant containing a single character that maps into a member of the basic execution character set is the numerical value of the representation of the mapped character interpreted as an integer. The value of an integer character constant containing more than one character, or containing a character or escape sequence not represented in the basic execution character set, is implementation-defined. In particular, in an implementation in which type char has the same range of values as signed char, the high-order bit position of a single-character integer character constant is treated as a sign bit.

这篇关于什么是一个枚举,其值显示为字符串类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆