评估 int 是否匹配定义的枚举类型

发布于 2024-12-16 22:16:47 字数 786 浏览 3 评论 0原文

我有一个包含多个类型定义的枚举 typedef,例如:

ActionTypeSomething = 1,
ActionTypeSomethingElse = 2

等等。

因此,我编写的方法评估传递的 int,然后相应地返回一个值(例如,字符串)。

(NSString *)evaluatAndReturnProperResult:(int)typeID

NSString *repsonseString;

switch (typeID) 
    case actionTypeSomething: {
        responseString = @"an appropriate string for typeID"
    }
...

return responseString;

因此,我的交换机评估每种支持的类型并返回正确的字符串。

现在我的问题是:

我只想返回受支持类型的字符串(即理论上可以传递任何整数)。如果没有匹配,我返回零。

显然我可以使用我已有的方法来做到这一点。但是,在通过 switch 发送它之前,我(理论上)是否可以通过评估传递的 int 来查看它是否与我定义的枚举类型匹配任何来提高性能(开关并不大,但我会如果我知道不会有匹配的话,仍然只是在方法的开头返回 nil )。

我确信这很简单,有人可以建议如何在输入开关之前评估我传递的整数是否与任何定义枚举 ActionType 匹配吗?在这种情况下,我可能过早地进行优化,但这更多的是一个关于如何实现它的一般问题(而不是我是否应该)。

I have an enum typedef containing several type definitions, eg:

ActionTypeSomething = 1,
ActionTypeSomethingElse = 2

And so on.

So a method I've written evaluates a passed int and then returns a value (for example, a string) accordingly.

(NSString *)evaluatAndReturnProperResult:(int)typeID

NSString *repsonseString;

switch (typeID) 
    case actionTypeSomething: {
        responseString = @"an appropriate string for typeID"
    }
...

return responseString;

So my switch evaluates each supported type and returns the correct string.

Now for my question:

I only want to return strings for supported types (i.e., in theory any integer could be passed). If there's no match, I return nil.

Obviously I can do this using exactly the method I already have. But could I (in theory) improve performance by evaluating the passed int to see if it matches any of my defined enum types BEFORE I send it through switch (the switch isn't massive, but I'd still rather just return nil at the beginning of the method if I know there's not going to be a match).

I'm sure this is easy, could someone suggest how to evaluate if my passed integer matches any define enum ActionType before I enter the switch? In this case I'm probably prematurely optimizing, but it's more of a general question about how to do achieve it (not if I should).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小伙你站住 2024-12-23 22:16:47

您可以再定义 2 个枚举值:

typedef enum {
    ActionTypeMin = 1,
    ActionTypeSomething = 1,
    ActionTypeSomethingElse = 2,
    ActionTypeMax = 2
} ActionType;

然后检查:

typeID >= ActionTypeMin && typeID <= ActionTypeMax

You can define 2 more enum values:

typedef enum {
    ActionTypeMin = 1,
    ActionTypeSomething = 1,
    ActionTypeSomethingElse = 2,
    ActionTypeMax = 2
} ActionType;

Then check:

typeID >= ActionTypeMin && typeID <= ActionTypeMax
戏舞 2024-12-23 22:16:47

您的方法的参数不应该是 int,理想情况下应该是您定义的枚举。这为您提供了一些编译时间检查。

如果这是不可能的,那么交换机中的默认情况将很好地处理它 - 这就是它们的设计目的。

The argument for your method shouldn't be an int, it should ideally be the enum you have defined. This gives you some compile time checking.

If this is not possible then your default case in the switch will handle it just fine - that's what they are designed for.

北座城市 2024-12-23 22:16:47

您正在使用 TypeDef 将 ActionEnum 限制为一组值,因此除了上行或下行通信之外,您不应在程序中使用 int 。有些人会说即使那样也不行,您应该接收一个字符串并将其映射到枚举,反之亦然。

就获得琴弦而言,通常的优化是
有一个从 ActionType1 到 ActionTypeN 的字符串数组。
使用枚举作为索引从数组中查找它。

该数组还将为您提供将字符串映射到枚举的操作。

一个简单的 if 语句将枚举转换为与数组边界相对应的整数,可以让您优雅地处理错误的值,尽管对我来说这应该抛出一个大的异常。

You are using a TypeDef that's to limit ActionEnum to a set of values, so you shouldn't be using int in your program except for up or downstream communication. Some would say not even then and that you should recive a string and map it to the enum and vice versa.

In terms of getting your strings the usual optimsatiin is
Have an array of strings from ActionType1 to ActionTypeN.
Use the enum as the index to look it up from the array.

The array will also give you the doings to map the string to the enum.

A simple if statement of the enum cast as an integer against teh bound of the array will let you deal gracefully with a bad value, though to me that should throw a big blaring exception.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文