Objective-C:从集合中获取最高的枚举类型
如果我有以下枚举类型:
typedef enum {Type1=0, Type2, Type3} EnumType;
以及以下代码(如果转换为 Java,则可以正常工作):
NSArray *allTypes = [NSArray arrayWithObjects:[NSNumber numberWithInt:Type1], [NSNumber numberWithInt:Type2], [NSNumber numberWithInt:Type3], nil];
EnumType aType = -1;
NSLog(@"aType is %d", aType); // I expect -1
// Trying to assign the highest type in the array to aType
for (NSNumber *typeNum in allTypes) {
EnumType type = [typeNum intValue];
NSLog(@"type is: %d", type);
if (type > aType) {
aType = type;
}
}
NSLog(@"aType is %d", aType); // I expect 2
结果日志为:
TestEnums[11461:b303] aType is: -1
TestEnums[11461:b303] type is: 0
TestEnums[11461:b303] type is: 1
TestEnums[11461:b303] type is: 2
TestEnums[11461:b303] aType is: -1
当我使用断点检查 aType 的值时,我看到:
aType = (EnumType) 4294967295
这是 根据 Wikipedia 32 位系统的最大 unsigned long int 值。
这是否意味着我无法将值分配给非枚举类型 在类型值的有效范围内?
为什么log(-1)的值与真实值不同 (4294967295)?和说明符(%d)有关系吗?
我怎样才能实现我在这里尝试做的事情而不添加新的 类型来表示无效值?请注意,集合可能 有时是空的,这就是为什么我在开始时使用 -1 来指示如果集合为空则没有类型。
注意:我是 Objective-C/ANSI-C 的新手。
谢谢, Mota
编辑:
这是我发现的一些奇怪的东西。如果我将循环内的条件更改为:
if (type > aType || aType == -1)
我会得到以下日志:
TestEnums[1980:b303] aType is -1
TestEnums[1980:b303] type is: 0
TestEnums[1980:b303] type is: 1
TestEnums[1980:b303] type is: 2
TestEnums[1980:b303] aType is 2
这正是我正在寻找的!奇怪的是 (aType == -1) 为何为 true,而 (Type1 > -1)、(Type2 > -1) 和 (Type3 > -1) 却不是?!
If I have the following enum type:
typedef enum {Type1=0, Type2, Type3} EnumType;
And the following code (which will work fine if converted to Java):
NSArray *allTypes = [NSArray arrayWithObjects:[NSNumber numberWithInt:Type1], [NSNumber numberWithInt:Type2], [NSNumber numberWithInt:Type3], nil];
EnumType aType = -1;
NSLog(@"aType is %d", aType); // I expect -1
// Trying to assign the highest type in the array to aType
for (NSNumber *typeNum in allTypes) {
EnumType type = [typeNum intValue];
NSLog(@"type is: %d", type);
if (type > aType) {
aType = type;
}
}
NSLog(@"aType is %d", aType); // I expect 2
The resulted logs are:
TestEnums[11461:b303] aType is: -1
TestEnums[11461:b303] type is: 0
TestEnums[11461:b303] type is: 1
TestEnums[11461:b303] type is: 2
TestEnums[11461:b303] aType is: -1
And when I inspect the value of aType using a breakpoint, I see:
aType = (EnumType) 4294967295
Which is according to Wikipedia the maximum unsigned long int value for 32-bit systems.
Does this mean that I cannot assign a value to enum types that is not
in the valid range of the type's values?Why is the value of the log (-1) differ from the real value
(4294967295)? Does it have something to do with the specifier (%d)?How can I achieve want I'm trying to do here without adding a new
type to represent an invalid value? Note that the collection may
sometimes be empty, this is why I'm using -1 at the beginning to indicate that there is no type if the collection was empty.
Note: I'm new to Objective-C/ANSI-C.
Thanks,
Mota
EDIT:
Here is something weird I've found. If I change the condition inside the loop to:
if (type > aType || aType == -1)
I get the following logs:
TestEnums[1980:b303] aType is -1
TestEnums[1980:b303] type is: 0
TestEnums[1980:b303] type is: 1
TestEnums[1980:b303] type is: 2
TestEnums[1980:b303] aType is 2
Which is exactly what I'm looking for! The weird part is how's (aType == -1) true, while (Type1 > -1), (Type2 > -1) and (Type3 > -1) are not?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎
EnumType
被定义为unsigned
类型。当您将其分配给-1
时,该值实际上会回滚到无符号 32 位整数的最高可能值(如您所发现的)。因此,通过从-1
开始值,您可以确保与它比较的其他值不可能更高,因为它被分配给最大值数据类型 (4294967295
)。我建议只在
0
处启动计数器,因为它是EnumType
的最低可能值。如果您想检查是否选择了任何值,您可以先检查集合的
计数
以查看是否有任何值。It seems like
EnumType
is defined to be anunsigned
type. When you assign it to-1
, this value actually rolls back to the highest possible value for an unsigned 32-bit integer (as you found). So, by starting the value at-1
, you are ensuring that no other value that you compare it to could possibly be higher, because it is assigned to the maximum value for the data type (4294967295
).I'd suggest just starting the counter at
0
instead, as it is the lowest possible value for anEnumType
.If you want to check to see if any value was chosen, you can check the
count
of the collection to see if there are any values first.