I've written an method that does some graphics calculations. There, you can specify a start-direction like "from left", "from right", "from bottom", "from top".
我写了一个方法来做一些图形计算。在那里,你可以指定一个起始方向,如“从左边”,“从右边”,“从底部”,“从顶部”。
Now I don't want the user of my method to pass confusing values like 1, 2, 3 or 4 or even strings. Nothing like that. Instead, I would like to create constants like:
现在我不希望我的方法的用户传递混乱的值,如1,2,3或4甚至字符串。没有那样的事。相反,我想创建常量,如:
kFromLeft, kFromRight, kFromTop, kFromBottom
kFromLeft,kFromRight,kFromTop,kFromBottom
I've seen this in an Apple header file:
我在Apple头文件中看到过这个:
enum CGImageAlphaInfo {
kCGImageAlphaNone,
kCGImageAlphaPremultipliedLast,
kCGImageAlphaPremultipliedFirst,
kCGImageAlphaLast,
kCGImageAlphaFirst,
kCGImageAlphaNoneSkipLast,
kCGImageAlphaNoneSkipFirst,
kCGImageAlphaOnly
};
typedef enum CGImageAlphaInfo CGImageAlphaInfo;
Five things I dont understand here / which are unclear to me:
我不明白的五件事/我不清楚:
1) Why is there a semicolon separating the definition from the typedef?
1)为什么有一个分号将定义与typedef分开?
2) Why do they repeat CGImageAlphaInfo like a parot?
2)为什么他们像parot一样重复CGImageAlphaInfo?
3) If I would put something like this in my header file, I would say in my method that the type of the parameter is CGImageAlphaInfo (of course I'll have a different name), right?
3)如果我在我的头文件中放入这样的东西,我会在我的方法中说参数的类型是CGImageAlphaInfo(当然我会有不同的名字),对吧?
4) I would normally specify the values for those constants in a way like that? (example):
4)我通常会以这样的方式指定这些常量的值? (例):
#define kCGImageAlphaNone 100
#define kCGImageAlphaPremultipliedLast 300
#define kCGImageAlphaPremultipliedFirst 900
5) Am I required to set those constants to such stupid values? Or could I just check inside my method which constant got passed in, like
5)我是否需要将这些常量设置为这样的愚蠢值?或者我可以在我的方法中检查哪个常量传入,比如
if(paramConst == kCGImageAlphaNone) {...}
?
5 个解决方案
#1
1) A semicolon always terminates an enum
statement. In this case there are two separate statements: one defines a named enumeration, the next defines a new type.
1)分号总是终止枚举语句。在这种情况下,有两个单独的语句:一个定义命名枚举,下一个定义新类型。
2) The enum statement creates a new type called "enum CGImageAlphaInfo
". But typing this everywhere is cumbersome, so the typedef statement is used. The typedef statement works like this:
2)enum语句创建一个名为“enum CGImageAlphaInfo”的新类型。但是到处输入这个很麻烦,所以使用了typedef语句。 typedef语句的工作方式如下:
typedef <sometype> <newname>;
typedef
So enum CGImageAlphaInfo
is the old type, and CGImageAlphaInfo
is the new name. Apple uses the same name for both, which is a bit confusing but is really the best way to go about it.
所以枚举CGImageAlphaInfo是旧类型,CGImageAlphaInfo是新名称。 Apple对两者使用相同的名称,这有点令人困惑,但实际上是最好的方法。
3) Right.
4) You can do this, but then you have to manually assign the constant values; with enum values are assigned automatically. The main benefit, though, is that you get some type checking since you can use the CGImageAlphaInfo
type instead of just a plain int which could be more easily assigned invalid values.
4)你可以这样做,但是你必须手动分配常数值;枚举值自动分配。但是,主要的好处是你得到了一些类型检查,因为你可以使用CGImageAlphaInfo类型而不仅仅是一个普通的int,它可以更容易地分配无效值。
5) I'm not sure what you mean by "stupid values". But yes, you should always check using the name in the way you describe, and never use some raw value like "300" or "1".
5)我不确定你的“愚蠢价值观”是什么意思。但是,是的,您应该始终使用您描述的方式检查名称,并且永远不要使用某些原始值,如“300”或“1”。
#2
Using an enum rather than a pre-processor define is the best way to handle this. For example,
使用枚举而不是预处理器定义是处理此问题的最佳方法。例如,
typedef enum
{
FromTop = 0,
FromBottom = 1,
FromLeft = 2,
FromRight = 3
} GraphicsLocation;
Put this in a Constants.h file and import it where needed. You actually don't need to include the =1, =2..., although it's useful if you need to specify the actual numeric value to use elsewhere (for example if you're setting the tag attribute of a menu item in Interface Builder).
将它放在Constants.h文件中并在需要时导入它。实际上您不需要包含= 1,= 2 ...,但如果您需要指定要在其他地方使用的实际数值(例如,如果您在接口中设置菜单项的标记属性)它很有用生成器)。
I've always declared enums like this, rather than the Apple example you posted above. Maybe someone more familiar with c can leave a comment explaining the difference.
我总是声明这样的枚举,而不是你上面发布的Apple示例。也许更熟悉c的人可以留下解释差异的评论。
#3
1) A semicolon is needed to terminate the enum
definition.
1)终止枚举定义需要分号。
2) CGImageAlphaInfo
is the name of the enum and the name of the defined type.
2)CGImageAlphaInfo是枚举的名称和定义类型的名称。
3) Right.
4) Using #define
for constants is often considered to be archaic C programming style. Declaring constants in an enum
gives static analyzers a more information than preprocessor macros do.
4)将#define用于常量通常被认为是古老的C编程风格。在枚举中声明常量为静态分析器提供了比预处理器宏更多的信息。
5) You should use the symbols, not numeric literals.
5)您应该使用符号,而不是数字文字。
#4
enum stands for Enumerated type.
枚举代表枚举类型。
This is a normal type-declaration with an in-line definition, it having a semi-colon at the end is syntactically correct.
这是一个带有内联定义的普通类型声明,它在结尾处有一个分号,在语法上是正确的。
No idea about the repeat, someone more familiar with this choice for Objective-C can answer this, probably.
不知道重复,有人更熟悉Objective-C的这个选择可能会回答这个问题。
The type should indeed be CGImageAlphaInfo.
该类型应该是CGImageAlphaInfo。
Well, normally is relative, in this case, using an enum for this is pretty normal.
嗯,通常是相对的,在这种情况下,使用枚举这是非常正常的。
In Java you would do this with checking for the parameters equality to the enum-symbol.
在Java中,您可以通过检查与枚举符号相等的参数来完成此操作。
#5
The things that are unclear to you:
你不清楚的事情:
1) Because the declaration of the enum is separate from the declaration of the type. In the sample code, the programmer first declares the enum, then declares the new type to use that enum.
1)因为枚举的声明与类型的声明是分开的。在示例代码中,程序员首先声明枚举,然后声明新类型以使用该枚举。
2) It might be easier if you look at it with (syntactically incorrect) quotes: typedef "enum CGImageAlphaInfo" CGImageAlphaInfo
. You're defining the type CGImageAlphaInfo
to be the same as the enum.
2)如果你用(语法上不正确的)引号来看它可能会更容易:typedef“enum CGImageAlphaInfo”CGImageAlphaInfo。您将CGImageAlphaInfo类型定义为与枚举相同。
3) Correct.
4) Your #define
method would work fine. Basically, enums are just another way of doing that kind of constant definition, but they're done by your compilation suite rather than having you pick constants to assign to each name. Each value in an enum is guaranteed to be distinct from the rest without you having to go through and make sure. They're also error-checked as necessary.
4)你的#define方法可以正常工作。基本上,枚举只是进行这种常量定义的另一种方式,但它们是由您的编译套件完成的,而不是让您选择常量来分配给每个名称。枚举中的每个值都保证与其他值不同,而不必经过并确保。必要时还会对它们进行错误检查。
#1
1) A semicolon always terminates an enum
statement. In this case there are two separate statements: one defines a named enumeration, the next defines a new type.
1)分号总是终止枚举语句。在这种情况下,有两个单独的语句:一个定义命名枚举,下一个定义新类型。
2) The enum statement creates a new type called "enum CGImageAlphaInfo
". But typing this everywhere is cumbersome, so the typedef statement is used. The typedef statement works like this:
2)enum语句创建一个名为“enum CGImageAlphaInfo”的新类型。但是到处输入这个很麻烦,所以使用了typedef语句。 typedef语句的工作方式如下:
typedef <sometype> <newname>;
typedef
So enum CGImageAlphaInfo
is the old type, and CGImageAlphaInfo
is the new name. Apple uses the same name for both, which is a bit confusing but is really the best way to go about it.
所以枚举CGImageAlphaInfo是旧类型,CGImageAlphaInfo是新名称。 Apple对两者使用相同的名称,这有点令人困惑,但实际上是最好的方法。
3) Right.
4) You can do this, but then you have to manually assign the constant values; with enum values are assigned automatically. The main benefit, though, is that you get some type checking since you can use the CGImageAlphaInfo
type instead of just a plain int which could be more easily assigned invalid values.
4)你可以这样做,但是你必须手动分配常数值;枚举值自动分配。但是,主要的好处是你得到了一些类型检查,因为你可以使用CGImageAlphaInfo类型而不仅仅是一个普通的int,它可以更容易地分配无效值。
5) I'm not sure what you mean by "stupid values". But yes, you should always check using the name in the way you describe, and never use some raw value like "300" or "1".
5)我不确定你的“愚蠢价值观”是什么意思。但是,是的,您应该始终使用您描述的方式检查名称,并且永远不要使用某些原始值,如“300”或“1”。
#2
Using an enum rather than a pre-processor define is the best way to handle this. For example,
使用枚举而不是预处理器定义是处理此问题的最佳方法。例如,
typedef enum
{
FromTop = 0,
FromBottom = 1,
FromLeft = 2,
FromRight = 3
} GraphicsLocation;
Put this in a Constants.h file and import it where needed. You actually don't need to include the =1, =2..., although it's useful if you need to specify the actual numeric value to use elsewhere (for example if you're setting the tag attribute of a menu item in Interface Builder).
将它放在Constants.h文件中并在需要时导入它。实际上您不需要包含= 1,= 2 ...,但如果您需要指定要在其他地方使用的实际数值(例如,如果您在接口中设置菜单项的标记属性)它很有用生成器)。
I've always declared enums like this, rather than the Apple example you posted above. Maybe someone more familiar with c can leave a comment explaining the difference.
我总是声明这样的枚举,而不是你上面发布的Apple示例。也许更熟悉c的人可以留下解释差异的评论。
#3
1) A semicolon is needed to terminate the enum
definition.
1)终止枚举定义需要分号。
2) CGImageAlphaInfo
is the name of the enum and the name of the defined type.
2)CGImageAlphaInfo是枚举的名称和定义类型的名称。
3) Right.
4) Using #define
for constants is often considered to be archaic C programming style. Declaring constants in an enum
gives static analyzers a more information than preprocessor macros do.
4)将#define用于常量通常被认为是古老的C编程风格。在枚举中声明常量为静态分析器提供了比预处理器宏更多的信息。
5) You should use the symbols, not numeric literals.
5)您应该使用符号,而不是数字文字。
#4
enum stands for Enumerated type.
枚举代表枚举类型。
This is a normal type-declaration with an in-line definition, it having a semi-colon at the end is syntactically correct.
这是一个带有内联定义的普通类型声明,它在结尾处有一个分号,在语法上是正确的。
No idea about the repeat, someone more familiar with this choice for Objective-C can answer this, probably.
不知道重复,有人更熟悉Objective-C的这个选择可能会回答这个问题。
The type should indeed be CGImageAlphaInfo.
该类型应该是CGImageAlphaInfo。
Well, normally is relative, in this case, using an enum for this is pretty normal.
嗯,通常是相对的,在这种情况下,使用枚举这是非常正常的。
In Java you would do this with checking for the parameters equality to the enum-symbol.
在Java中,您可以通过检查与枚举符号相等的参数来完成此操作。
#5
The things that are unclear to you:
你不清楚的事情:
1) Because the declaration of the enum is separate from the declaration of the type. In the sample code, the programmer first declares the enum, then declares the new type to use that enum.
1)因为枚举的声明与类型的声明是分开的。在示例代码中,程序员首先声明枚举,然后声明新类型以使用该枚举。
2) It might be easier if you look at it with (syntactically incorrect) quotes: typedef "enum CGImageAlphaInfo" CGImageAlphaInfo
. You're defining the type CGImageAlphaInfo
to be the same as the enum.
2)如果你用(语法上不正确的)引号来看它可能会更容易:typedef“enum CGImageAlphaInfo”CGImageAlphaInfo。您将CGImageAlphaInfo类型定义为与枚举相同。
3) Correct.
4) Your #define
method would work fine. Basically, enums are just another way of doing that kind of constant definition, but they're done by your compilation suite rather than having you pick constants to assign to each name. Each value in an enum is guaranteed to be distinct from the rest without you having to go through and make sure. They're also error-checked as necessary.
4)你的#define方法可以正常工作。基本上,枚举只是进行这种常量定义的另一种方式,但它们是由您的编译套件完成的,而不是让您选择常量来分配给每个名称。枚举中的每个值都保证与其他值不同,而不必经过并确保。必要时还会对它们进行错误检查。